Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading