pandas 熊猫系列均值和标准差

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/42933372/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 03:15:10  来源:igfitidea点击:

Pandas series mean and standard deviation

pythonpandas

提问by magicsword

I have a list:

我有一个清单:

data = [
{'A': [2.0, 3.0, 4.0, 5.0, 6.0], 'B':[27.0, 28.0, 29.0, 30.0], 'C': ['lic1'],
 'D': ['soy1'], 'E': ['foo1']},
{'A': [7.0, 11.0, 90.0, 43.0, 87.0], 'B':[27.0, 28.0, 29.0, 30.0], 'C': ['lic1'],
 'D': ['soy1'], 'E': ['foo1']},
# ... etc

]

]

The data on 'A' is a Pandas Series. I would like to compute the average and standard deviation for the data in 'A' (there are several records for A) for example: (mean=(2.0+3.0+4.0+5.0+6.0+7.0+11.0+90.0+43.0+87.0)/len(A)=25.8)

'A' 上的数据是 Pandas 系列。我想计算“A”中数据的平均值和标准偏差(A 有几条记录),例如:(mean=(2.0+3.0+4.0+5.0+6.0+7.0+11.0+90.0+43.0+ 87.0)/len(A)=25.8)

采纳答案by jezrael

You can use list comprehensionwith concatand then meanor std.

您可以使用list comprehensionwithconcat和 thenmeanstd

For converting to float(int) add astype, if still problem need to_numericwith parameter errors='coerce'.

对于转换为float( int) add astype,如果仍然有问题需要to_numeric参数errors='coerce'

s = pd.concat([pd.Series(x['A']) for x in data]).astype(float)
print (s)
0     2.0
1     3.0
2     4.0
3     5.0
4     6.0
0     7.0
1    11.0
2    90.0
3    43.0
4    87.0
dtype: float64

print (s.mean())
25.8

print (s.std())
35.15299892375234

Another solution:

另一种解决方案:

from  itertools import chain

s = pd.Series(list(chain.from_iterable([x['A'] for x in data]))).astype(float)
print (s)
0     2.0
1     3.0
2     4.0
3     5.0
4     6.0
5     7.0
6    11.0
7    90.0
8    43.0
9    87.0
dtype: float64