如何从字典中的多个列表中计算均值和SD并将它们整理到新的字典中?

问题描述 投票:0回答:3
i有一个包含5个键的字典,每个密钥都以列表的形式分配了10个值:

{'a': [5, 6, 3, 1, 3, 2, 5, 8, 6, 7], 'b': [3, 5, 2, 7, 0, 2, 10, 4, 3, 4], 'c': [9, 7, 11, 10, 8, 9, 7, 10, 7, 9], 'd': [6, 4, 5, 7, 6, 8, 5, 6, 5, 7], 'e': [2, 5, 1, 4, 2, 3, 4, 2, 5, 1]}
我还设置了一个空词典,我想在原始字典中存储每个列表的平均值和标准偏差:

{'a_analysis': [], 'b_analysis': [], 'c_analysis': [], 'd_analysis': [], 'e_analysis': []}

到目前为止,我有此代码:
for key in original_dictionary:   #for each key in the dictionary
   for value in original_dictionary:   #iterate through the values in each key
      sum =+ value   #add the value to a sum variable
   mean = sum / len(orignal_dictionary[key])   #get the mean by dividing the sum by the len of each key
   #here I want to return the mean value to the respective key in the new dictionary 
   sd =    #then I need to get a value for the standard deviation here 
   #and also return it to the respective key in the new dictionary

任何帮助将不胜感激。
	

您也可以使用

pandas
python list dictionary for-loop
3个回答
1
投票

import pandas as pd out = pd.DataFrame(your_dict).agg(['mean', 'std']).to_dict()

输出:

{'a': {'mean': 4.6, 'std': 2.2705848487901865},
 'b': {'mean': 4.0, 'std': 2.8284271247461903},
 'c': {'mean': 8.7, 'std': 1.4181364924121764},
 'd': {'mean': 5.9, 'std': 1.1972189997378646},
 'e': {'mean': 2.9, 'std': 1.5238839267549948}}

这里是一个解决方案,希望您喜欢这样:

0
投票

这是输出:

{'a_analysis': {'mean': 4.6, 'standard deviation': 2.1540659228538015},
 'b_analysis': {'mean': 4.0, 'standard deviation': 2.6832815729997477},
 'c_analysis': {'mean': 8.7, 'standard deviation': 1.345362404707371},
 'd_analysis': {'mean': 5.9, 'standard deviation': 1.1357816691600546},
 'e_analysis': {'mean': 2.9, 'standard deviation': 1.445683229480096}}

刚刚完成了我的解决方案,并添加了一些评论。
现在可以解释。

0
投票

最新问题
© www.soinside.com 2019 - 2025. All rights reserved.