pandas fillna: How to fill only leading NaN from beginning of series until first value appears?

Question:

I have several pd.Series that usually start with some NaN values until the first real value appears. I want to pad these leading NaNs with 0, but not any NaNs that appear later in the series.

pd.Series([nan, nan, 4, 5, nan, 7])

should become

ps.Series([0, 0, 4, 5, nan, 7])
Asked By: ascripter

||

Answers:

Use first_valid_index with loc:

s.loc[:s.first_valid_index()] = 0

Or mask with isnull and forward filling NaNs:

s = s.mask(s.ffill().isnull(), 0)

print (s)
0    0.0
1    0.0
2    4.0
3    5.0
4    NaN
5    7.0
dtype: float64

EDIT: For function per groups use:

def func(x):
    x['col1'] = x['col1'].mask(x['col1'].ffill().isnull(), 0)
    return x


df = df.groupby('col').apply(func)
Answered By: jezrael
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.