Informatics Practices · Ch 3 — Data Handling using Pandas – II
Calculating Standard Deviation
Calculating Standard Deviation
The standard deviation tells you how spread out the numbers in a dataset are from their average (mean). A small standard deviation means the values are clustered close to the mean; a large one means they are widely scattered. In Pandas, you can calculate it for any numeric column or DataFrame using the .std() method.
Using DataFrame.std()
The method DataFrame.std() returns the standard deviation of the values in each column. Internally, it first calculates the variance (the average of the squared differences from the mean) and then takes its square root.
For example, if you have a DataFrame df with columns for student marks in different subjects, you can get the standard deviation for a selection of those columns:
df[['Maths','Science','S. St','Hindi','Eng']].std()
The output will be a Series showing the standard deviation for each subject. For the textbook's example data, the result is shown below.
This tells you, for instance, that Maths marks have a standard deviation of about 3.98, while Science marks are less spread out, with a standard deviation of about 2.67.
The describe() Function — A One-Stop Summary
Instead of calculating each statistic separately, you can use DataFrame.describe() to get a whole set of descriptive statistical values in a single command. This function is extremely useful for getting a quick overview of your data.
When you call df.describe(), it automatically includes:
- count — the number of non-null values in each column
- mean — the average value
- std — the standard deviation
- min — the smallest value
- 25% — the first quartile (25th percentile)
- 50% — the median (50th percentile)
- 75% — the third quartile (75th percentile)
- max — the largest value
For the textbook's example DataFrame, df.describe() produces the following table. …
| Column | Std. Deviation |
|---|---|
| Maths | 3.980064 |
| Science | 2.667140 |
| S.St | 3.146667 |
| Hindi | 3.157483 |
| UT | Maths | Science | S.St | Hindi | Eng | |
|---|---|---|---|---|---|---|
| count | 12.000000 | 12.000000 | 12.00000 | 12.000000 | 12.000000 | 12.000000 |
| mean | 2.000000 | 19.250000 | 19.75000 | 20.416667 | 21.833333 | 20.500000 |
| std | 0.852803 | 3.980064 | 2.66714 | 3.146667 | 3.157483 | 3.370999 |
| min | 1.000000 | 12.000000 | 15.00000 | 15.000000 | 15.000000 | 13.000000 |
| 25% | 1.000000 | 16.500000 | 18.00000 | 18.750000 | 20.750000 | 19.750000 |
| 50% | 2.000000 | 20.500000 | 19.50000 | 20.000000 | 22.500000 | 21.500000 |