简体   繁体   English

熊猫系列的平均值和标准差

[英]Pandas series mean and standard deviation

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. “A”的数据是熊猫系列。 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'中数据的平均值和标准差(例如,A有几个记录):(均值=(2.0 + 3.0 + 4.0 + 5.0 + 6.0 + 7.0 + 11.0 + 90.0 + 43.0 + 87.0)/ LEN(A)= 25.8)

You can use list comprehension with concat and then mean or std . 您可以将list comprehensionconcat ,然后使用meanstd

For converting to float ( int ) add astype , if still problem need to_numeric with parameter errors='coerce' . 为了转换为floatint )添加astype ,如果仍然有问题需要带参数errors='coerce' to_numeric

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM