+2 votes
in Programming Languages by (14.7k points)
I want to compute the mean and standard deviation for each column of a pandas DataFrame. Is there any function in the pandas module for it?

1 Answer

0 votes
by (25.6k points)

You can use the describe() function of the pandas module. It returns the basic summary of the data in each column and the summary includes mean and standard deviation too.

Here is an example:

>>> import pandas as pd
>>> aa={'A':[1,2,3,4], 'B':[11,12,13,14], 'C':[21,22,23,24]}
>>> aa
{'A': [1, 2, 3, 4], 'B': [11, 12, 13, 14], 'C': [21, 22, 23, 24]}
>>> df = pd.DataFrame(aa)
>>> df
   A   B   C
0  1  11  21
1  2  12  22
2  3  13  23
3  4  14  24
>>> df.describe()
              A          B          C
count  4.000000   4.000000   4.000000
mean   2.500000  12.500000  22.500000
std    1.290994   1.290994   1.290994
min    1.000000  11.000000  21.000000
25%    1.750000  11.750000  21.750000
50%    2.500000  12.500000  22.500000
75%    3.250000  13.250000  23.250000
max    4.000000  14.000000  24.000000

...