Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pandas series mean and standard deviation

Tags:

python

pandas

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)

like image 750
magicsword Avatar asked Mar 21 '17 16:03

magicsword


People also ask

How do you take the mean of a series in pandas?

Algorithm. Step 1: Define a Pandas series. Step 2: Use the mean() function to calculate the mean. Step 3: Print the mean.

How do you find the standard deviation of a series in Python?

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.

What is STD () in pandas?

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.

How do you find the mean of a series in Python?

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.


1 Answers

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
like image 123
jezrael Avatar answered Sep 21 '22 06:09

jezrael