Issue
I want to calculate the max value in the past 3 rolling rows, ignoring NaN
if I see them. I assumed that skipna
would do that, but it doesn't. How can I ignore NaN
, and also what is skipna
supposed to do?
In this code
import pandas as pd
df = pd.DataFrame({'sales': [25, 20, 14]})
df['max'] = df['sales'].rolling(3).max(skipna=True)
print(df)
The last column is
sales max
0 25 NaN
1 20 NaN
2 14 25.0
But I want it to be
sales max
0 25 25.0
1 20 25.0
2 14 25.0
Solution
You can also use Series.bfill
with your command:
df['max'] = df['sales'].rolling(3).max().bfill()
Output:
sales max
0 25 25.0
1 20 25.0
2 14 25.0
Answered By - Mayank Porwal
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.