Real text data is messy. Names arrive with inconsistent capitalization, emails carry stray spaces, categories are spelled three different ways, and one column crams together values that belong in two. Cleaning this up is a large share of every analyst's time, and pandas makes it manageable through the .str accessor, which applies string operations to an entire column at once. This tutorial covers the methods you will use on almost every text column.
The core idea is that .str gives a text Series the same methods you know from plain Python strings, but vectorized, so df["name"].str.lower() lowercases thousands of values in one call, no loop required.
The str accessor
Any column of text, stored as object or the newer string dtype, gains string methods through .str. Start with a small illustrative sample that has deliberately messy values:
import pandas as pd
df = pd.DataFrame({
"name": [" Aarti ", "BHASKAR", "chitra", " Devan"],
"email": ["aarti@gmail.com", "bhaskar@yahoo.com",
"chitra@gmail.com", "devan@outlook.com"],
})
df["name_clean"] = df["name"].str.strip().str.title()
print(df[["name", "name_clean"]])
name name_clean
0 Aarti Aarti
1 BHASKAR Bhaskar
2 chitra Chitra
3 Devan Devan
Two methods chained together, strip to drop the surrounding spaces and title to standardize capitalization, cleaned the whole column at once. Chaining .str methods like this is the everyday pattern for tidying text.
Testing and filtering text
str.contains tests each value for a substring and returns a boolean Series, which is perfect for filtering. Here we keep only Gmail users:
gmail = df[df["email"].str.contains("gmail", na=False)]
print(gmail[["name_clean", "email"]])
name_clean email
0 Aarti aarti@gmail.com
2 Chitra chitra@gmail.com
Two options matter: na=False treats missing values as non-matches so the filter does not error, and case=False makes the match case-insensitive. Related tests include startswith, endswith and str.len() for length.
Replacing and cleaning
str.replace swaps text, and by default it interprets its pattern as a regular expression, which is powerful for cleaning:
prices = pd.Series(["₹1,200", "₹850", "₹1,500"])
clean = prices.str.replace("₹", "", regex=False).str.replace(",", "", regex=False)
print(clean.astype(int))
0 1200
1 850
2 1500
dtype: int64
By stripping the currency symbol and thousands separators, a text column of prices becomes real integers you can compute on. This "strip the noise, then convert the type" sequence is one of the most common cleaning tasks in analytics.
Splitting and extracting
Often one column holds two facts. str.split with expand=True breaks it into separate columns:
full = pd.DataFrame({"full_name": ["Aarti Rao", "Bhaskar Iyer", "Chitra Nair"]})
parts = full["full_name"].str.split(" ", expand=True)
full["first"] = parts[0]
full["last"] = parts[1]
print(full)
full_name first last
0 Aarti Rao Aarti Rao
1 Bhaskar Iyer Bhaskar Iyer
2 Chitra Nair Chitra Nair
For pulling a specific pattern out of text, str.extract with a regular expression captures exactly the piece you want, such as the domain from an email:
df["domain"] = df["email"].str.extract(r"@(\w+)\.")
print(df[["email", "domain"]])
email domain
0 aarti@gmail.com gmail
1 bhaskar@yahoo.com yahoo
2 chitra@gmail.com gmail
3 devan@outlook.com outlook
The parentheses in the pattern mark the capture group, so pandas returns only the domain word between the @ and the dot. Together, split and extract turn a single cluttered column into clean, analysable fields, and they are how you derive new dimensions, like an email provider or a product code, from text you already have.
Practical usage
Text cleaning is usually the first stage of a project, before any counting or grouping is trustworthy. A typical sequence: strip whitespace, standardize case with lower or title, replace out stray symbols, then convert types. Standardizing case is what makes "Pune", "pune" and "PUNE" group together correctly instead of counting as three cities. When categories are inconsistent, a map dictionary paired with cleaned text collapses them to a canonical form. Because every .str method is vectorized, this cleaning stays fast even on large datasets.
Common mistakes
- Comparing uncleaned text. " Pune" and "Pune" look identical but are not equal, so they land in separate groups. Always
stripand normalize case before grouping or joining on text. - Forgetting
na=Falseincontains. With missing values present,str.containsreturns NaN and the filter raises an error. Addna=Falseto treat NaN as no match. - Unexpected regex behaviour.
str.replacetreats its pattern as regex by default, so characters like.or$behave specially. Passregex=Falsefor a literal replacement, or escape the special characters. - Splitting without
expand=True. Plainstr.splitreturns a column of lists, not new columns. Useexpand=Truewhen you want separate columns.
In interviews
Data-cleaning questions are extremely common because interviewers know it is most of the job. Expect a messy column and a request like "standardize these city names" or "extract the email domain," which test whether you reach for the .str accessor rather than a Python loop. A frequent gotcha is a filter that breaks on missing values, checking whether you know na=False. Demonstrating clean, chained .str operations and awareness of the regex default marks you as someone who has wrangled real, imperfect data.
Where this fits in your learning path
String cleaning is a practical, high-frequency skill in the data analytics path. It works hand in hand with apply and map for recoding categories, and cleaned text is what makes filtering rows reliable. Once text is clean you often move on to datetime handling for any date fields hidden in strings. Turning messy text into analysable data is a daily reality of the data analyst role.
Frequently Asked Questions
What is the str accessor in pandas?
How do I check if a column contains a substring in pandas?
How do I split a text column into multiple columns?
How do I remove extra spaces from text in pandas?
Why does str.contains raise an error on missing values?
Want to Build Your Career in Data Analytics with AI?
Join CodeBegun and train with working industry engineers — Check the Data Analyst training details

