基于Python键的多个字典中的平均值? [英] Average value in multiple dictionaries based on key in Python?

查看:138
本文介绍了基于Python键的多个字典中的平均值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有三个字典(或更多):

I have three dictionaries (or more):

A = {'a':1,'b':2,'c':3,'d':4,'e':5}
B = {'b':1,'c':2,'d':3,'e':4,'f':5}
C = {'c':1,'d':2,'e':3,'f':4,'g':5}

如何获取三个字典中每个键的平均值的字典?

How can I get a dictionary of the average values of every key in the three dictionaries?

例如,给定上述字典,输出将为:

For example, given the above dictionaries, the output would be:

{'a':1/1, 'b':(2+1)/2, 'c':(3+2+1)/3, 'd':(4+3+2)/3, 'e':(5+4+3)/3, 'f':(5+4)/2, 'g':5/1}


推荐答案

您可以使用熊猫,如下所示:

You can use Pandas, like this:

import pandas as pd
df = pd.DataFrame([A,B,C])
answer = dict(df.mean())
print(answer)

这篇关于基于Python键的多个字典中的平均值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆