Reputation: 171
I have a pandas dataframe in python that has NaN values. If a row has an NaN value then I want to replace the entire row with the preceding row.
So this
stock label open high low close
0 CAT 09:31 AM 137.090 137.175 137.090 137.175
1 CAT 09:32 AM NaN -1.000 -1.000 NaN
Would become this
stock label open high low close
0 CAT 09:31 AM 137.090 137.175 137.090 137.175
1 CAT 09:32 AM 137.090 137.175 137.090 137.175
Any help is greatly appreciated!
Upvotes: 0
Views: 72
Reputation: 323226
Using mask
and isnull
to mask all row as NaN then we using ffill
df=df.set_index(['stock','label'])
df=df.mask(df.isnull().any(1)).ffill().reset_index()
df
Out[889]:
stock label open high low close
0 CAT 09:31AM 137.09 137.175 137.09 137.175
1 CAT 09:32AM 137.09 137.175 137.09 137.175
Upvotes: 4