Python lesson
Cleaning text
strip removes the spaces at each end, then title capitalises each word: ' ada obi' becomes 'Ada Obi'.
Try this lesson free →Example
This customer list has stray spaces and odd capitals. .str applies text methods to every value in a column at once:
df["Name"] = df["Name"].str.strip().str.title()
dfName City Revenue
0 Ada Obi lagos 70
1 Tunde Bakare ABUJA 15
2 Grace Eze abuja NaN
3 Musa Bello Kano 40
4 Musa Bello Kano 40Key points
.str.strip()Remove spaces at the start and end of every value.
.str.title()Capitalise each word.
.str.upper() .str.lower()All capitals, or all lower case.
Cleaning comes before analysis: 'abuja' and 'ABUJA' would otherwise count as two different cities.
Practice
1. Build a line that fixes the capitals in the City column.
df["City"] = df["City"].str.title()2. Build a line that removes the extra spaces from the Name column.
df["Name"] = df["Name"].str.strip()3. Clean the Name column: remove the extra spaces, then make it title case.
df["Name"] = df["Name"].str.strip().str.title()4. Make every City title case.
df["City"] = df["City"].str.title()5. Make every City all capitals.
df["City"] = df["City"].str.upper()Learn it by doing. This lesson is a short hands-on level on Bampaa: see it work, then build and write it yourself, with instant feedback.
Try this lesson free →