spg719
spg719

Reputation: 105

How do I forward fill on specific rows only in Pandas?

I have the following df:

   col1  col2  col3
0    a   1.0   1.0
1    b   NaN   NaN   
2    c   5.0   3.0
3    d   5.0   NaN

I want to do forward fill the rows only where col2 = NaN so the result would be like this

   col1  col2  col3
0    a   1.0   1.0
1    b   1.0   1.0   
2    c   5.0   3.0
3    d   5.0   NaN

I was able to get to here but this isn't a smart solution because it ffills row 3 even though it doesn't meet the requirements of col2 = NaN

df['col2'] = df['col2'].fillna(method="ffill")
df['col3'] = df['col3'].fillna(method="ffill")

Upvotes: 2

Views: 562

Answers (2)

mozway
mozway

Reputation: 260900

Another approach using boolean indexing for in place filling:

df.loc[df['col2'].isna()] = df.ffill()

Output:

  col1  col2  col3
0    a   1.0   1.0
1    b   1.0   1.0
2    c   5.0   3.0
3    d   5.0   NaN

Upvotes: 3

Panda Kim
Panda Kim

Reputation: 13247

Code

df.fillna(df.ffill().where(df['col2'].isna()))

output:

    col1    col2    col3
0   a       1.0     1.0
1   b       1.0     1.0
2   c       5.0     3.0
3   d       5.0     NaN

Example

import pandas as pd
data = {'col1': ['a', 'b', 'c', 'd'], 'col2': [1, None, 5, 5], 'col3': [1, None, 3, None]}
df = pd.DataFrame(data)

Upvotes: 4

Related Questions