Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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

>Solution :

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)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading