Search code examples
pythonpandasdataframefillna

Iterate over a column using fillna


I have the following df that repeats itself in the Day column:

     Day    Value
0     0      1.0
1   NaN      1.6
2   NaN      1.0
3   NaN      1.7
4   NaN      6.0
5     0      9.0
6   NaN      7.8
7   NaN      2.5
8   NaN      1.2
9   NaN      3.4
...

I would like to fillna incrementing +1 in each row. Thus getting this output:

    Day     Value
0     0       1.0
1     1       1.6
2     2       1.0
3     3       1.7
4     4       6.0
5     0       9.0
6     1       7.8
7     2       2.5
8     3       1.2
9     4       3.4
...

I tried df.Day.fillna(value = dfs.iloc[-1]['Day] + 1, inplace = True)

But it's not working.


Solution

  • Calculate the cumulative summation with cumsum.

    df.fillna(1, inplace=True)
    df['Day'] = df['Day'].groupby((df['Day']- df['Day'].shift()!=0).cumsum()).cumsum()
    
    df
        Day Value
    0   0.0 1.0
    1   1.0 1.6
    2   2.0 1.0
    3   3.0 1.7
    4   4.0 6.0
    5   0.0 9.0
    6   1.0 7.8
    7   2.0 2.5
    8   3.0 1.2
    9   4.0 3.4