5
votes

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)

1
all_as = reduce( (lambda x, y: x['A'] + y['A']), data) - greedy52

1 Answers

5
votes

You can use list comprehension with concat and then mean or std.

For converting to float (int) add astype, if still problem need to_numeric with parameter 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