Home > OS >  How to do sum previous and next row with skipping Nan in Pandas
How to do sum previous and next row with skipping Nan in Pandas

Time:07-14

I have one column in my Dataframe and I am trying to calculate the energy loss with formula. Problem is that I want to use only two valid rows each time where values are not NaN. Energy is the input column and looking for something like loss column.

Energy loss
NaN Nan
NaN Nan
NaN Nan
4 Nan
NaN Nan
3 1/2(4^2-3^2)
NaN Nan
11 Nan
3 1/2(3^2-11^2)
NaN NaN
14 Nan

I tried Lambda custom function but not able to send the next row.

CodePudding user response:

Try something like this:

df = pd.DataFrame({'Energy':[4,None,3,None,11,3,None,14]})
energy = df.Energy.dropna()
def my_loss(series):
    return 1/2*(series.iloc[0]**2-series.iloc[1]**2)
loss = energy.rolling(2).apply(my_loss)
df['loss'] = loss[1::2]  # skip half of the results

Basically you apply your custom function in the rolling of the droped nan energy, and then merge it again with your df.

  • Related