Issue
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])
Solution
Use first_valid_index
with loc
:
s.loc[:s.first_valid_index()] = 0
Or mask
with isnull
and forward filling NaN
s:
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
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.