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?
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 .
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.
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.
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
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