4

I have a data frame like this.

Name    Roll    GPA
A       10      5.0
B       NaN     4.5
C       12      NaN

I am using:

df.fillna(method='ffill', inplace=True)

But it fills all the columns using ffill method.

But I want to fill the NaN value using both ffill & bfill method. For example, I want to apply method='ffill' to the Roll column and method='bfill' to the GPA column. How can I do this?

cs95
  • 379,657
  • 97
  • 704
  • 746
Adnan Toky
  • 1,756
  • 2
  • 11
  • 19

1 Answers1

4

You can do both.

df2 = df.assign(Roll=df.Roll.ffill(), GPA=df.GPA.bfill())
cs95
  • 379,657
  • 97
  • 704
  • 746