25. How to Rank and Running Totals in pandas (rank, cumsum, window)
25.7 Practice, Tip and Close
Why. Rank + cumsum + one rolling mean should feel boring before interview day.
What. One lab on fictional Maharashtra delivered orders.
How.
Chapter practice lab
- Build a DataFrame of ≥12 delivered orders across Pune, Nashik, Kolhapur, Solapur with dates 20–24 Oct 2026 and amounts.
- Add global dense rank on
amount_inrand city dense rank. - Build a daily GMV table; add running GMV overall and per city.
- Add a 3-day rolling mean of daily orders (statewide).
- Export
outputs/rank_cumsum_lab.csvfor the order-level ranks (index=False). - Write three WhatsApp lines: top order, Pune running GMV on 24 Oct, rolling mean vs raw spike day.
from pathlib import Path
import pandas as pd
Path("outputs").mkdir(exist_ok=True)
orders = pd.DataFrame({
"order_id": [f"BLK-{i}" for i in range(1, 13)],
"city": ["Pune", "Pune", "Nashik", "Kolhapur", "Solapur", "Pune",
"Nashik", "Pune", "Nagpur", "Kolhapur", "Solapur", "Pune"],
"order_date": pd.to_datetime(
["2026-10-20"] * 3 + ["2026-10-21"] * 3 + ["2026-10-22"] * 2
+ ["2026-10-23"] * 2 + ["2026-10-24"] * 2
),
"amount_inr": [90, 210, 95, 180, 70, 220, 110, 150, 200, 340, 80, 210],
})
orders["rank_global"] = orders["amount_inr"].rank(ascending=False, method="dense")
orders["rank_city"] = (
orders.groupby("city")["amount_inr"].rank(ascending=False, method="dense")
)
orders.to_csv("outputs/rank_cumsum_lab.csv", index=False)
daily = (
orders.groupby("order_date", as_index=False)["amount_inr"]
.sum()
.rename(columns={"amount_inr": "gmv"})
.sort_values("order_date")
)
daily["gmv_running"] = daily["gmv"].cumsum()
daily["gmv_roll3"] = daily["gmv"].rolling(3).mean()
print(orders.head())
print(daily)
Practice task
Amir: “Only Pune + Nashik. City rank 1 rows. Also city running GMV by date.” Print both tables. No ML.
Ravindra Bagale's Tip
Remember three simple ideas: rank shows position, cumsum shows the total so far, and rolling shows the last N days. Never forget to sort before you use them. Under every rank, write the metric and the peer group, such as “among Pune stores this week”.
Ravindra Bagale's Tip – मराठी
तीन सोप्या गोष्टी लक्षात ठेवा: rank स्थान दाखवतो, cumsum आतापर्यंतची बेरीज दाखवतो, आणि rolling शेवटचे N दिवस दाखवतो. हे वापरण्याआधी sort करायला कधीही विसरू नका. प्रत्येक rank खाली metric आणि तुलना केलेला group लिहा, जसं “या आठवड्यातील Pune चे stores”.
Ravindra Bagale's Tip – हिंदी
तीन आसान बातें याद रखो: rank जगह दिखाता है, cumsum अब तक का total दिखाता है, और rolling आख़िरी N दिन दिखाता है। इन्हें इस्तेमाल करने से पहले sort करना कभी मत भूलो। हर rank के नीचे metric और तुलना वाला group लिखो, जैसे “इस हफ़्ते के Pune stores में”।
Thodkyaat sangaycha tar (quick recap)
- Rank labels ties; pick
dense/min/averageon purpose - Cumsum needs correct sort order
- Groupby for within-city rank and running totals
- Rolling = fixed window; expanding = growing window
- Check grain (day vs order) before rolling
- Export ranks as new columns; keep raw amounts
Samajla ka? Aata leaderboard ani running GMV pandas madhe clear yenar — pudhe ek chhote lab / day.