Home > Blockchain >  pandas summing rows before NaN condition is encountered
pandas summing rows before NaN condition is encountered

Time:02-03

I have a pandas df of the format below:


    Value1   Value2   Value3
     -10      -20      NaN
     -5       -10      20
     -15       -10     NaN   
     -20       -5      15

I am looking to do a rolling sum of Value1 and Value2 based on the NaN encountered in Value3 So the final result looks like:

Value1    Value2    Value3
-15       -30        20
-35      -15         15

Here each row is a cumulative sum for (Value1 and Value2) of the values from the last time Value3 had a non NaN value.

Except for looping through I am out of ideas on how to achieve this...any thoughts? Thanks!

CodePudding user response:

Try:

out = df.groupby(df['Value3'].isna().cumsum()).sum().reset_index(drop=True)
print(out)

# Output
   Value1  Value2  Value3
0     -15     -30    20.0
1     -35     -15    15.0

Focus on groups:

>>> df['Value3'].isna().cumsum()
0    1  # group1, rows 1 and 2
1    1
2    2  # group2, rows 3 and 4
3    2
Name: Value3, dtype: int64
  •  Tags:  
  • Related