Mathematical and Statistical Methods
After inspecting the Series, we may want to perform calculations on the marks. Pandas provides several methods for mathematical and statistical operations.
sum(): The sum() method returns the sum of the non-missing values in the Series.
For example:
marks = pd.Series([85, 72, 90, 65])
marks.sum()
output:
312
The method adds all the values together:
85 + 72 + 90 + 65 = 312
mean(): The mean() method returns the average of the values in the Series.
For example:
marks = pd.Series([80, 90, 70])
marks.mean()
output:
80.0
The average is (80 + 90 + 70) / 3 = 80.
So, whenever we want to find the average of numerical data, we can use mean().
median(): The median() method returns the middle value after arranging the values in ascending order.
For example:
marks = pd.Series([60, 70, 80, 90, 100])
marks.median()
output:
80.0
The values are already arranged as 60, 70, 80, 90, 100, so the middle value is 80.
mode(): The mode() method returns the value or values that occur most frequently in the Series.
For example:
marks = pd.Series([70, 80, 80, 90, 80])
marks.mode()
output:
0 80
dtype: int64
Here, 80 occurs most frequently, so mode() returns 80.
min(): The min() method returns the smallest value from the Series.
For example:
marks = pd.Series([85, 72, 90, 65])
marks.min()
output:
65
So, the smallest mark is 65.
max(): The max() method returns the largest value from the Series.
For example:
marks = pd.Series([85, 72, 90, 65])
marks.max()
output:
90
So, the largest mark is 90.
count(): The count() method returns the number of non-null values present in the Series.
For example:
marks = pd.Series([85, 72, 90, 65, None])
marks.count()
output:
4
There are 5 total items, but one value is missing. So, count() returns 4 because it does not count the missing value.
If we want the total number of items, including missing values, we can use the size attribute.
marks.size
output:
5
So remember:
size → total number of items
count() → number of non-null items
std(): The std() method returns the standard deviation of the values.
For example:
marks = pd.Series([60, 70, 80, 90, 100])
marks.std()
output:
15.811388300841896
Standard deviation helps us understand how much the values are spread out from their average.
For beginners, you can simply remember:
std() → tells us how much the data varies.
var(): The var() method returns the variance of the values.
For example:
marks = pd.Series([60, 70, 80, 90, 100])
marks.var()
output:
250.0
Variance is also used to understand how much the values are spread out.
quantile(): The quantile() method returns the value at a particular quantile.
For example:
marks = pd.Series([60, 70, 80, 90, 100])
marks.quantile(0.5)
output:
80.0
Here, 0.5 represents the 50th percentile, which is closely related to the median.
We can also use:
marks.quantile(0.25)
output:
70.0
Here, 0.25 represents the 25th percentile.
cumsum(): The cumsum() method returns the cumulative sum of the values.
For example:
marks = pd.Series([10, 20, 30, 40])
marks.cumsum()
output:
0 10
1 30
2 60
3 100
dtype: int64
Here, each value is added to the sum of the previous values.
The calculation is:
10
10 + 20 = 30
30 + 30 = 60
60 + 40 = 100
cumprod(): The cumprod() method returns the cumulative product of the values.
For example:
marks = pd.Series([2, 3, 4])
marks.cumprod()
output:
0 2
1 6
2 24
dtype: int64
The calculation is:
2
2 × 3 = 6
6 × 4 = 24
cummax(): The cummax() method returns the maximum value seen so far as we move through the Series.
For example:
marks = pd.Series([60, 80, 70, 90])
marks.cummax()
output:
0 60
1 80
2 80
3 90
dtype: int64
At each position, Pandas keeps the highest value encountered so far.
cummin(): The cummin() method works similarly to cummax(), but it keeps track of the minimum value seen so far.
For example:
marks = pd.Series([60, 80, 50, 90])
marks.cummin()
output:
0 60
1 60
2 50
3 50
dtype: int64