[英]Find average using dictionary key
I have these columns in my dataframe: 我的数据框中有这些列:
I need to find average of all the [Count] values where Day is 1
or 2
and so on. 我需要找到Day为
1
或2
的所有[Count]值的平均值,依此类推。
I've tried that by using key dictionary: 我已经尝试通过使用密钥字典:
col_dict = dict(zip(df6.Day, df6.Count))
k=2 # Enter Day
#print(k)
for keys in col_dict.keys():
if keys == k:
for i in col_dict:
a=col_dict[keys] where col_dict==k
But I think this code is not correct. 但是我认为这段代码是不正确的。
Pandas has purpose-built methods for grouping and aggregating data. Pandas具有专门用于分组和汇总数据的方法。 Here you can use
groupby
+ mean
: 在这里,您可以使用
groupby
+ mean
:
res = df6.groupby('Day')['Count'].mean()
There is no need for dictionary conversion or explicit iteration when vectorised operations are available. 当向量化操作可用时,无需字典转换或显式迭代。 If you need a dictionary result, you can convert in a separate step:
如果需要字典结果,可以在单独的步骤中进行转换:
res_dict = res.to_dict()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.