Overview
" Paris", "paris " and "PARIS" are one city. Normalize a text column with .str.strip() and .str.lower() before you count anything.
- .str gives string methods to a column
- strip() and lower()
- Counting after cleaning
Short Course
.str gives every string method to a whole column:
s.str.strip().str.lower(). Trap: until you normalize, " Paris", "paris"
and "PARIS" count as three different cities.
cities = pd.Series([" Paris", "paris", "PARIS ", "Lyon"])
cities.nunique() # 4
cities.str.strip().str.lower().nunique() # 2
Task
Write city_counts(s) that returns a dict of how many times each city
appears after trimming spaces and lower-casing, for example
[" Paris", "paris", "Lyon"] gives {'paris': 2, 'lyon': 1}.
Test Suites
Pending
Next
Output