Pandas Mean 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 'A' data represents the Pandas series. I would like to calculate the mean and standard deviation for the data in "A" (for example, several records for A): (mean value = (2.0 + 3.0 + 4.0 + 5.0 + 6.0 + 7.0 + 11.0 + 90.0 + 43.0 + 87.0) / LEN (A) = 25.8)

+4
source share
1 answer

You can use list comprehensionwith concat, and then meanor std.

To convert to float( int), add astypeif you still need a to_numericparameter problem 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
+4
source

Source: https://habr.com/ru/post/1672849/


All Articles