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 to do a fillna with zero values until data appears in each column, then use the forward fill for each column in pandas data frame

I have the following data frame,

date          x1     x2     x3  
2001-01-01   nan    0.4    0.1 
2001-01-02   nan    0.3    nan
2021-01-03   nan    nan    0.5
...
2001-05-05   nan    0.1    0.2
2001-05-06   0.1    nan    0.3
...

So I want to first fill all nan values with zero until the first data point appears in each column, after that, I want the rest of the rows to use the fowardfill function.

So the above data frame should look like this,

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

date          x1     x2     x3  
2001-01-01     0    0.4    0.1 
2001-01-02     0    0.3    0.1
2021-01-03     0    0.3    0.5

...
2001-05-05     0    0.1    0.2
2001-05-06   0.1    0.1    0.3
...

If I do fillna with 0 first then do forwardfill, like this,

df = df.fillna(0)
df = df.ffill()

I just get all the na values to be zero, and I am unable to do ffill for the parts where the data starts.

Is there a way to do the ffill the way I want?

>Solution :

Reverse the logic:

out = df.ffill().fillna(0)
print(out)

# Output
         date   x1   x2   x3
0  2001-01-01  0.0  0.4  0.1
1  2001-01-02  0.0  0.3  0.1
2  2021-01-03  0.0  0.3  0.5
3  2001-05-05  0.0  0.1  0.2
4  2001-05-06  0.1  0.1  0.3
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