Data Science · Level 3 Cleaning

Cleaning Text with pandas .str

" 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

.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

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}.

solution.py
Test Suites Pending
Output