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.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, I am filling in col_A missing values like this:
df = df.groupby([df.index.month, df.index.day]).transform(lambda x: x.fillna(x.mean()))
However, when I do this, it makes col_B go away. How can I retain col_B which is all strings?
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
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments