python - Retaining a column with all strings during groupby on a pandas dataframe -


datetime    col_a   col_b 1/1/2012    125.501  1/2/2012    nan      1/3/2012    125.501  1/4/2013    nan      1/5/2013    125.501  b 2/28/2013   125.501  b 2/28/2014   125.501  b 1/2/2016    125.501  b 1/4/2016    125.501  b 2/28/2016   nan      b 

as per fill in missing values in pandas dataframe using mean, filling in col_a missing values this:

df = df.groupby([df.index.month, df.index.day]).transform(lambda x: x.fillna(x.mean())) 

however, when this, makes col_b go away. how can retain col_b strings?

i think 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     2012-01-02  125.501     2012-01-03  125.501     2013-01-04  125.501     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 

Comments