pandas challenges
Labelled data: indexes, filtering, and split-apply-combine.
109 exercises
1. Foundations
2. Selection
3. Cleaning
Missing datalesson 9
Duplicateslesson 10
Text with .strlesson 11
- Return the counts of item after normalising the text, so ginger shows 5 rather…1
- Return every row whose item contains "ginger", regardless of case2
- Split orders["date"] on "-" into separate columns and return the resulting frame3
- Pull the four-digit year out of orders["date"] using .str.extract() with a capt…4
4. Aggregation
Summarising a tablelesson 13
GroupBy: split, apply, combinelesson 14
agg, transform and applylesson 15
- Group orders by city and return a frame with total (sum of cups) and best (max…1
- Return a Series giving each row's cups as a fraction of its city's total — same…2
- Keep only the rows belonging to cities whose total cups is more than 4003
- Return the total cups per city with city as a column rather than the index4
pivot_table and crosstablesson 16
- Build a pivot table of total cups with city down the side and item across the t…1
- The same table, but with the empty combinations shown as 0 instead of NaN2
- Now the average rating per city and item, with city down the side3
- Return a count of how many orders each city placed for each item, using pd.cros…4
5. Combining & reshaping
Stacking tables with concatlesson 17
merge and joinlesson 18
Wide and long formlesson 19
MultiIndex, stack and unstacklesson 20
- Group orders by city and item and return the total cups — a Series with a Multi…1
- Take that Series and unstack it, filling the empty combinations with 02
- From the grouped Series, return every city's masala total — cutting across the…3
- Flatten the grouped Series back into an ordinary frame with city, item and cups…4
6. Time series
Dates and timeslesson 21
resample and rollinglesson 22
7. In practice
Categories and memorylesson 24
Method chaininglesson 25
- In a single chain, add a price column (revenue / cups) to orders and return the…1
- Chain it: drop duplicates, then keep only rows with more than 100 cups, then re…2
- One chain: add price, then keep only rows where price is above 203
- Use .pipe() to apply len to orders after dropping duplicates — returning the ro…4
8. Capstones
Capstone: clean a messy datasetcapstone
- Start with an audit: return how many values are missing in each column of orders1
- Return orders with the exact duplicate removed2
- After deduplicating, normalise item to lower case and return the counts — ginge…3
- Fill the missing rating values with the mean rating for that city, and return t…4
- Build the whole thing as one chain and return its shape: deduplicate, lower-cas…5
Capstone: a time series reportcapstone
- Return the average cups per weekday name — the "normal week" answer1
- Return the 7-day rolling mean of sales, which cancels the weekly rhythm and lea…2
- Return the weekly totals with the partial first and last buckets dropped — comp…3
- Return the average week-on-week change — each day compared with the same day la…4
- Return the three busiest days in sales, largest first5