Stats, Cumulative & Rounding
Find who scored the most with idxmax/idxmin, summarise with describe and agg, track running totals with cumsum/cumprod/cummax/cummin, and tidy values with clip and round.
After exploring a Series, you usually want answers: who top-scored, what is the typical score, how did the total build up over by over, and how do you cap or round values for a clean report. This topic covers the statistical and transformation methods you will use most.
Definition
idxmax/idxmin return the label of the largest/smallest value. describe returns a summary of statistics in one Series. Cumulative methods (
cumsum,cumprod,cummax,cummin) return a Series of the same length where each position holds the running total/product/maximum/minimum up to that point. clip limits values to a range, and round rounds them to a number of decimals.
- Label vs position —
idxmax()gives a label ("Pant");argmax()gives a position (3). - Running — each output value depends on all the values before and including it.
- Same length — cumulative methods,
clipandrounddo not reduce the Series; they transform each element.
Syntax
python
s.idxmax() s.idxmin() s.argmax() s.argmin()
s.describe(percentiles=None)
s.agg(["min", "max", "mean"])
s.cumsum() s.cumprod() s.cummax() s.cummin()
s.clip(lower=None, upper=None)
s.round(decimals=0)| Method / parameter | Meaning | Default |
|---|---|---|
idxmax() / idxmin() | label of the max / min value (first one if tied) | — |
argmax() / argmin() | integer position of the max / min | — |
describe(percentiles=[...]) | which percentiles to show | [.25, .5, .75] |
agg(func or list) | apply one or several aggregations by name | — |
cumsum() etc. — skipna | skip NaN while accumulating | True |
clip(lower, upper) | values below lower become lower, above upper become upper | None (no limit) |
round(decimals) | decimal places to keep (negative rounds to tens, hundreds…) | 0 |
Example — idxmax / idxmin vs argmax / argmin
python
import pandas as pd
runs = pd.Series([45, 78, 12, 101, 33],
index=["Rohit", "Virat", "Gill", "Pant", "Hardik"])
print(runs.idxmax(), runs.idxmin())
print(runs.argmax(), runs.argmin())
print(runs.max(), runs[runs.idxmax()])text
Pant Gill
3 2
101 101How it works: max() tells you the top score (101), idxmax() tells you who scored it (Pant), and argmax() tells you where it is (position 3). Use idxmax with .loc, and argmax with .iloc.
Example — describe
python
print(runs.describe())text
count 5.000000
mean 53.800000
std 35.604775
min 12.000000
25% 33.000000
50% 45.000000
75% 78.000000
max 101.000000
dtype: float64| Row | Meaning |
|---|---|
count | number of non-missing values |
mean | average |
std | standard deviation (spread around the mean; uses n-1) |
min / max | smallest / largest |
25% | first quartile — 25% of values are ≤ this |
50% | the median — half the values are ≤ this |
75% | third quartile — 75% of values are ≤ this |
For a text Series, describe shows different statistics:
python
teams = pd.Series(["CSK", "MI", "KKR", "MI", "CSK", "MI"])
print(teams.describe())text
count 6
unique 3
top MI
freq 3
dtype: objectunique = number of distinct values, top = most frequent value, freq = how often it appears.
Example — cumulative methods: an innings over by over
python
over_runs = pd.Series([8, 4, 12, 6, 15, 9], index=[1, 2, 3, 4, 5, 6])
print(pd.DataFrame({"runs": over_runs,
"cumsum": over_runs.cumsum(),
"cummax": over_runs.cummax(),
"cummin": over_runs.cummin()}))text
runs cumsum cummax cummin
1 8 8 8 8
2 4 12 8 4
3 12 24 12 4
4 6 30 12 4
5 15 45 15 4
6 9 54 15 4How it works:
cumsum— the team score after each over: 8, 8+4=12, 12+12=24, …cummax— the best over so far: it only changes when a new record is set (12 in over 3, 15 in over 5).cummin— the quietest over so far.
cumprod multiplies instead of adding — useful for growth factors:
python
growth = pd.Series([1.10, 1.05, 0.95, 1.20]) # season-on-season change in followers
print(growth.cumprod().round(4))text
0 1.1000
1 1.1550
2 1.0973
3 1.3167
dtype: float64After four seasons the follower count is 1.3167 × the original (+31.67%).
Example — clip
python
print(runs.clip(lower=20, upper=80))text
Rohit 45
Virat 78
Gill 20
Pant 80
Hardik 33
dtype: int64Gill's 12 was raised to the lower limit 20, Pant's 101 was cut to the upper limit 80; values in between are unchanged. clip is used to cap outliers (e.g. treat any score above the 99th percentile as that percentile). Pass only one side to cap one end: runs.clip(upper=50).
Example — round
python
sr = pd.Series([138.4567, 145.2049, 129.9951])
print(sr.round(1))
print(sr.round())text
0 138.5
1 145.2
2 130.0
dtype: float64
0 138.0
1 145.0
2 130.0
dtype: float64round(1) keeps one decimal; round() (= round(0)) rounds to whole numbers but the dtype stays float (138.0). Convert with .astype(int) if you need integers.
Note: pandas (like NumPy) uses "round half to even":
pd.Series([2.5, 3.5]).round()gives2.0, 4.0. This avoids bias when rounding many values.
Comparison
| Method | Returns | Length |
|---|---|---|
max() / min() | a value | 1 number |
idxmax() / idxmin() | a label | 1 label |
argmax() / argmin() | a position | 1 integer |
describe() | summary Series | 8 rows (numeric) / 4 rows (text) |
cumsum(), cummax()… | Series | same as input |
clip(), round() | Series | same as input |
Common mistakes
- ❌ Using
max()when you need who scored most. ✅idxmax()returns the label. - ❌ Mixing
argmax()with.loc. ✅argmax→.iloc;idxmax→.loc. - ❌ Expecting
round()to produce integers. ✅ It stays float; add.astype(int). - ❌
s.clip(20, 80)on a text Series. ✅clipis for numbers (or other orderable types like dates).
Interview tip
Q: Difference between idxmax() and argmax() on a Series?
A: idxmax() returns the index label of the maximum value; argmax() returns its integer position. They only coincide when the index is the default RangeIndex.
Key points to remember
- idxmax()/idxmin() return the label of the max/min; argmax()/argmin() return its position.
- describe() gives count, mean, std, min, quartiles (25/50/75%) and max for numbers.
- For text, describe() gives count, unique, top (most frequent) and freq.
- cumsum/cumprod/cummax/cummin return running totals/products/max/min of the same length.
- clip(lower, upper) caps values to a range — handy for taming outliers.
- round(n) rounds to n decimals but keeps float dtype; pandas rounds half to even.
- agg(['min', 'max', 'mean']) computes several statistics at once.
Try it yourself
Who top-scored? idxmax and describe
Innings progression with cumulative methods
clip and round for a clean report
Practice: run chase tracker
Revision Flashcards
Tap a card to flip it and see the answer.
