Retaining A Column With All Strings During Groupby On A Pandas Dataframe
datetime col_A col_B 1/1/2012 125.501 A 1/2/2012 NaN A 1/3/2012 125.501 A 1/4/2013 NaN A 1/5/2013 125.501 B 2/28/2013 125.501 B 2/28/2014 125.
Solution 1:
I think you can add col_A
:
df['col_A'] = df.groupby([df.index.month, df.index.day])['col_A'].transform(lambda x:
x.fillna(x.mean()))
print df
col_A col_B
datetime
2012-01-01 125.501 A
2012-01-02 125.501 A
2012-01-03 125.501 A
2013-01-04 125.501 A
2013-01-05 125.501 B
2013-02-28 125.501 B
2014-02-28 125.501 B
2016-01-02 125.501 B
2016-01-04 125.501 B
2016-02-28 125.501 B
Post a Comment for "Retaining A Column With All Strings During Groupby On A Pandas Dataframe"