Vasilis
Vasilis

Reputation: 85

Need to fill NaN with next values in Pandas Dataframe

I have this dataframe

enter image description here

My goal is to fill the Nan values in the 3rd and 4th column with the non-Nan data in each column. Tried shift(-5) but didn't get it to work. Any thoughts?

Upvotes: 0

Views: 35

Answers (1)

Mayank Porwal
Mayank Porwal

Reputation: 34046

Use bfill:

df[['col3','col4']] = df[['col3','col4']].bfill()

OR:

df[['col3','col4']] = df[['col3','col4']].fillna(method='bfill')

Upvotes: 1

Related Questions