Python - Mean(), Median(), Mode()
In statistics, a Central Tendency is a central or typical value for a probability distribution. Measures of central tendency help you find the middle, or the average, or frequent value of a data set. Mean , Median and the Mode are commonly used measures of central tendency.- Mean : The mean value is the average value.
- Median : The median value is the value in the middle, after you have sorted all the values.
- Mode : The Mode value is the value that appears the most number of times.

Lets create a DataFrame...
df = pd.DataFrame([[32, 24, 30, 40], [17, 24, 21, 28], [50, 25, 28, 32],
[25, 34, 21, 48], [17, 31, 18, 28], [35, 24, 19, 42]],
columns=['Physics', 'Chemistry', 'Biology', 'Maths'],
index=['Student-1', 'Student-2', 'Student-3', 'Student-4',
'Student-5', 'Student-6'])
Physics Chemistry Biology Maths
Student-1 32 24 30 40
Student-2 17 24 21 28
Student-3 50 25 28 32
Student-4 25 34 21 48
Student-5 17 31 18 28
Student-6 35 24 19 42
Calculate Mean
df.mean()
Physics 29.333333
Chemistry 27.000000
Biology 22.833333
Maths 36.333333
If you want to find out the mean of a single column in a DataFrame:
df['column_name'].mean()
df['Chemistry'].mean()
27.0
By default, mean is calculated every single column (axis=0) in the DataFrame. If you Pass the argument of (axis=1) will return the mean of every single row in the DataFrame .
df.mean(axis=1)
Student-1 31.50
Student-2 22.50
Student-3 33.75
Student-4 32.00
Student-5 23.50
Student-6 30.00
Calculate Median
df.median()
Physics 28.5
Chemistry 24.5
Biology 21.0
Maths 36.0
If you want to find out the median of a single column in a DataFrame:
df['column_name'].median()
df['Chemistry'].median()
24.5
By default, median is calculated every single column (axis=0) in the DataFrame. If you Pass the argument of (axis=1) will return the median of every single row in the DataFrame .
df.median(axis=1)
Student-1 31.0
Student-2 22.5
Student-3 30.0
Student-4 29.5
Student-5 23.0
Student-6 29.5
Calculate Mode
df.mode()
Physics Chemistry Biology Maths
0 17 24 21 28
Summary Statistics
This function gives you several useful things all at the same time.
df.describe()
Physics Chemistry Biology Maths
count 6.000000 6.00000 6.000000 6.000000
mean 29.333333 27.00000 22.833333 36.333333
std 12.564500 4.38178 4.956477 8.238123
min 17.000000 24.00000 18.000000 28.000000
25% 19.000000 24.00000 19.500000 29.000000
50% 28.500000 24.50000 21.000000 36.000000
75% 34.250000 29.50000 26.250000 41.500000
max 50.000000 34.00000 30.000000 48.000000
Related Topics