Quantcast
Channel: How to replace NaNs with some change in previous values in Pandas DataFrame? - Stack Overflow
Browsing index pages (3 articles)

How to replace NaNs with some change in previous values in Pandas DataFrame?

0 1 20 10 20 301 40 NaN 602 50 55 903 60 NaN 804 70 75 90What I need to do is replace every NaN value with 30 , 65 respectively. That means ten added to previous value

View Article


Answer by anky for How to replace NaNs with some change in previous values in...

You can shift the dataframe and then add 10 , then fillna with that df:df = df.fillna(df.shift().add(10))# for a new df :-> new_df = df.fillna(df.shift().add(10))print(new_df) 0 1 20 10 20.0 301 40...

View Article


Answer by Ashwin Agarwal for How to replace NaNs with some change in previous...

You can try this as well:df = df.fillna(df.fillna(method='ffill').add(10))I find this method easier.

View Article
Browsing index pages (3 articles)


Latest Images