Python - 查找字典元素的平均值

Rhy*_*Guy 2 python dictionary average

我有这样的字典:

dict = [{'a':2, 'b':3}, {'b':4}, {'a':1, 'c':5}]
Run Code Online (Sandbox Code Playgroud)

我需要获得所有不同键的平均值。结果应如下所示:

avg = [{'a':1.5, 'b':3.5, 'c':5}]
Run Code Online (Sandbox Code Playgroud)

我可以得到所有键的摘要,但我没有意识到如何计算相同的键以获得平均数。

MSe*_*ert 6

轻松完成:

>>> import pandas
>>> df = pandas.DataFrame([{'a':2, 'b':3}, {'b':4}, {'a':1, 'c':5}])
>>> df.mean()
a    1.5
b    3.5
c    5.0
dtype: float64
Run Code Online (Sandbox Code Playgroud)

如果您需要字典作为结果:

>>> dict(df.mean())
{'a': 1.5, 'b': 3.5, 'c': 5.0}
Run Code Online (Sandbox Code Playgroud)