📊 Unit 1 – DataFrame · Lesson 12
Descriptive Statistics
Sample data
Descriptive statistics summarise data with a few numbers – the average, the middle, the spread. pandas has a function for each.
Python
import pandas as pd
df = pd.DataFrame({'Name': ['Asha', 'Ravi', 'Neha', 'Om', 'Zoya', 'Kabir'],
'Maths': [78, 92, 65, 88, 92, 54],
'Science': [81, 76, 90, 70, 85, 62],
'IP': [88, 81, 95, 74, 81, 69]})
df.to_csv('marks.csv', index=False)
print(df) Name Maths Science IP
0 Asha 78 81 88
1 Ravi 92 76 81
2 Neha 65 90 95
3 Om 88 70 74
4 Zoya 92 85 81
5 Kabir 54 62 69
max, min, sum, count
Python
import pandas as pd
df = pd.read_csv('marks.csv')
m = df[['Maths', 'Science', 'IP']]
print(m.max())
print(m.min())
print('Sum of IP:', m['IP'].sum(), '| count:', m['IP'].count())Maths 92
Science 90
IP 95
dtype: int64
Maths 54
Science 62
IP 69
dtype: int64
Sum of IP: 488 | count: 6
mean, median, mode
Python
import pandas as pd
df = pd.read_csv('marks.csv')
m = df[['Maths', 'Science', 'IP']]
print(m.mean().round(2))
print(m.median())
print(m.mode()) # most frequent value(s) in each columnMaths 78.17
Science 77.33
IP 81.33
dtype: float64
Maths 83.0
Science 78.5
IP 81.0
dtype: float64
Maths Science IP
0 92.0 62 81.0
1 NaN 70 NaN
2 NaN 76 NaN
3 NaN 81 NaN
4 NaN 85 NaN
5 NaN 90 NaN
In Maths 92 appears twice, so the mode is 92. In Science every value appears once, so all values are modes; IP's mode is 81.
quantile, var, std
Python
import pandas as pd
df = pd.read_csv('marks.csv')
print(df['Maths'].quantile([0.25, 0.5, 0.75]))
print('variance:', round(df['Maths'].var(), 2))
print('std dev :', round(df['Maths'].std(), 2))0.25 68.25
0.50 83.00
0.75 91.00
Name: Maths, dtype: float64
variance: 247.37
std dev : 15.73
Row-wise statistics (axis=1)
axis = 0 (default)
- Down each column
- One result per column
- df.mean()
axis = 1
- Across each row
- One result per row
- df.mean(axis=1)
Python
import pandas as pd
df = pd.read_csv('marks.csv', index_col='Name')
print(df.sum(axis=1)) # row-wise total per student
print(df.mean(axis=1).round(1).idxmax(), 'has the best average')Name
Asha 247
Ravi 249
Neha 250
Om 232
Zoya 258
Kabir 185
dtype: int64
Zoya has the best average
describe()
Python
import pandas as pd
df = pd.read_csv('marks.csv')
print(df.describe().round(2)) Maths Science IP
count 6.00 6.00 6.00
mean 78.17 77.33 81.33
std 15.73 10.23 9.35
min 54.00 62.00 69.00
25% 68.25 71.50 75.75
50% 83.00 78.50 81.00
75% 91.00 84.00 86.25
max 92.00 90.00 95.00
Summary table
| Function | Gives |
|---|---|
| max() / min() | Largest / smallest value |
| sum() / count() | Total / number of non-NaN values |
| mean() | Average |
| median() | Middle value |
| mode() | Most frequent value(s) |
| quantile(q) | Value at fraction q of sorted data |
| var() / std() | Variance / standard deviation |
| describe() | count, mean, std, min, 25%, 50%, 75%, max together |
Frequently Asked Questions
Why can mode() return more than one row?
If two values occur equally often, both are modes. pandas lists all of them; columns with fewer modes show NaN in the extra rows.
What is the 0.25 quantile?
The value below which 25% of the data lies (first quartile, Q1). The 0.5 quantile is the median.
Does pandas std() divide by n or n-1?
By n-1 (sample standard deviation) by default. Use std(ddof=0) to divide by n.
