Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Standard deviation for DF, pandas

for example I have a pandas DataFrame, which looks as:

a b c
1 2 3
4 5 6
7 8 9

I want to calculate the standard deviation for all values in this DF. The function df.std() get me back the values pro column.

Of course I can create the next code:

sd = []
sd.append(list(df['a']))
sd.append(list(df['b']))
sd.append(list(df['c']))
numpy.std(sd)

Is it possible to do this code simpler and use some pandas function for this DF?

like image 323
Guforu Avatar asked Apr 22 '15 13:04

Guforu


People also ask

How do you calculate standard deviation in Pandas DataFrame?

Standard deviation is calculated using the function . std() . However, the Pandas library creates the Dataframe object and then the function . std() is applied on that Dataframe .

How do you find the standard deviation of a row in Pandas?

We can get stdard deviation of DataFrame in rows or columns by using std(). Int (optional ), or tuple, default is None, standard deviation among all the elements.

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

stdev() method calculates the standard deviation from a sample of data. Standard deviation is a measure of how spread out the numbers are. A large standard deviation indicates that the data is spread out, - a small standard deviation indicates that the data is clustered closely around the mean.


1 Answers

df.values returns a NumPy array containing the values in df. You could then apply np.std to that array:

In [52]: np.std(sd)
Out[52]: 2.5819888974716112

In [53]: np.std(df.values)
Out[53]: 2.5819888974716112
like image 124
unutbu Avatar answered Oct 09 '22 19:10

unutbu