python - Group rows by date and overwrite NaN values -
i have dataframe of following structure simplified question.
b c d e 0 2014/01/01 nan nan 0.2 nan 1 2014/01/01 0.1 nan nan nan 2 2014/01/01 nan 0.3 nan 0.7 3 2014/01/02 nan 0.4 nan nan 4 2014/01/02 0.5 nan 0.6 0.8
what have here series of readings across several timestamps on single days. columns b,c,d , e represent different locations. data reading in set such @ specified timestamp takes data locations , fills in nan values other locations.
what wish group data timestamp can .groupby()
command. there wish have nan values in grouped data overwritten valid values taken in later rows such following result obtained.
b c d e 0 2014/01/01 0.1 0.3 0.2 0.7 1 2014/01/02 0.5 0.4 0.6 0.8
how go achieving this?
try df.groupby
dataframegroupby.agg
:
in [528]: df.groupby('a', as_index=false, sort=false).agg(np.nansum) out[528]: b c d e 0 2014/01/01 0.1 0.3 0.2 0.7 1 2014/01/02 0.5 0.4 0.6 0.8
a shorter version dataframegroupby.sum
(thanks maxu!):
in [537]: df.groupby('a', as_index=false, sort=false).sum() out[537]: b c d e 0 2014/01/01 0.1 0.3 0.2 0.7 1 2014/01/02 0.5 0.4 0.6 0.8
Comments
Post a Comment