python - Looking for a pandas function analogous to DataFrame.nafill() -
i apply function acts fillna()
takes different value nan. unfortunately dataframe.replace()
not work in case. here example: given dataframe:
df = pd.dataframe([[1,2,3],[4,-1,-1],[5,6,-1]]) 0 1 2 0 1 2.0 3.0 1 4 -1.0 -1.0 2 5 6.0 -1.0 3 7 8.0 nan
i looking function output:
0 1 2 0 1 2.0 3.0 1 4 2.0 3.0 2 5 6.0 3.0 3 7 8.0 nan
so df.replace()
to_replace=-1
, 'method='ffill'
not work because requires column-independent value
replace -1 entries. in example column-dependent. know can code loop looking efficient code applied large dataframe. suggestions? thank you.
you can replace value nan
, call ffill
:
in [3]: df.replace(-1, np.nan).ffill() out[3]: 0 1 2 0 1 2 3 1 4 2 3 2 5 6 3
i think you're on thinking this
edit
if have nan
values create boolean mask , update elements again ffill
on inverse of mask:
in [15]: df[df == -1] = df[df != -1].ffill() df out[15]: 0 1 2 0 1 2 3 1 4 2 3 2 5 6 3 3 7 8 nan
another method (thanks @dsm in comments) use where
same thing above:
in [17]: df.where(df != -1, df.replace(-1, np.nan).ffill()) out[17]: 0 1 2 0 1 2 3 1 4 2 3 2 5 6 3 3 7 8 nan
Comments
Post a Comment