简体   繁体   中英

Pandas- Fill nans up until first non NULL value

I have a dataframe like

A    B    C
1    nan  nan
2    nan  5
3    3    nan
4    nan  nan

How do I only fill the NULLs (with 0) for each series up until the first non NULL value, leading to

A    B    C
1    0    0
2    0    5
3    3    nan
4    nan  nan

Bit of a trick using pandas.DataFrame.ffill with notna and where :

df.where(df.ffill().notna(), 0)

Or using pandas.DataFrame.interpolate :

df.interpolate('zero', fill_value=0, limit_direction='backward')

Output:

   A    B    C
0  1  0.0  0.0
1  2  0.0  5.0
2  3  3.0  NaN
3  4  NaN  NaN

This would be done using where or mask .

df.mask(df.notna().cumsum().eq(0), 0)
# or,
df.where(df.notna().cumsum().ne(0), 0)

   A    B    C
0  1  0.0  0.0
1  2  0.0  5.0
2  3  3.0  NaN
3  4  NaN  NaN

Many ways to skin a cat here:-)

Since 0 + nan is nan this works:

xf = df.fillna(0) + df.bfill()*0

Good Answers above. Alternately, if you want to do it for a specific column:

df[columnName][:df[columnName].first_valid_index()].fillna(0, inplace=True)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM