pandas Groupby Mean与Nan [英] pandas groupby mean with nan
本文介绍了 pandas Groupby Mean与Nan的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有以下数据框:
date id cars
2012 1 4
2013 1 6
2014 1 NaN
2012 2 10
2013 2 20
2014 2 NaN
现在,我想对多年来忽略不计NaN的每个ID求出汽车的平均值.结果应该是这样的:
Now, I want to get the mean of cars over the years for each id ignoring the NaN's. The result should be like this:
date id cars result
2012 1 4 5
2013 1 6 5
2014 1 NaN 5
2012 2 10 15
2013 2 20 15
2014 2 NaN 15
我有以下命令:
df["result"]=df.groupby("id")["cars"].mean()
该命令运行无误,但结果列仅包含NaN. 我做错了什么?
The command runs without errors, but the result column only has NaN's. What did I do wrong?
推荐答案
Use transform
, this returns a series the same size as the original:
df["result"]=df.groupby("id")["cars"].transform('mean')
print (df)
date id cars result
0 2012 1 4.0 5.0
1 2013 1 6.0 5.0
2 2014 1 NaN 5.0
3 2012 2 10.0 15.0
4 2013 2 20.0 15.0
5 2014 2 NaN 15.0
这篇关于 pandas Groupby Mean与Nan的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文