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)
Algorithm. Step 1: Define a Pandas series. Step 2: Use the mean() function to calculate the mean. Step 3: Print the mean.
std() function to find the standard deviation of the given Series object. Output : Now we will use Series. std() function to find the standard deviation of the given Series object.
std() The Pandas std() is defined as a function for calculating the standard deviation of the given set of numbers, DataFrame, column, and rows. In respect to calculate the standard deviation, we need to import the package named "statistics" for the calculation of median.
mean() function to find the mean of the underlying data in the given series object. Output : Now we will use Series. mean() function to find the mean of the given series object.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With