4

I have a dataframe with a column of sequential but not adjacent numbers and missing values.

I'd like to use the fillna function to fill in the missing values with an incremented value from the previous non-missing row.

Here's a simplified table:

index  my_counter
0      1
1      2
2      NaN
3      3
4      NaN
5      NaN
6      8

I'd like to fill in my_counter as such:

index  my_counter
0      1
1      2
2      2.1
3      3
4      3.1
5      3.2
6      8

How can I accomplish this task?

Grant Miller
  • 27,532
  • 16
  • 147
  • 165
Eric M
  • 109
  • 1
  • 6
  • Hi @EricM, could you please show your code as well? What have you tried so far? – toti08 Sep 20 '18 at 05:46
  • My attempt (which was successful) involved looping and comparing, which is not the panda-centric solution I was seeking. Wen's solution below works quite well. Just need to add that you need to set my_column equal to that line of code since the intention was to "replace" the NaN with the incremented values. – Eric M Sep 21 '18 at 17:07

1 Answers1

8

IIUC ffill with groupby cumcount

df.my_counter.ffill()+df.groupby(df.my_counter.notnull().cumsum()).cumcount()/10
Out[92]: 
0    1.0
1    2.0
2    2.1
3    3.0
4    3.1
5    3.2
6    8.0
dtype: float64
BENY
  • 317,841
  • 20
  • 164
  • 234