4 min read min read
String Manipulation Basics
Learn basic text operations in pandas
String Manipulation Basics
What is String Data?
String means text. In real data, text is often messy:
- Extra spaces: " John "
- Wrong case: "JOHN" or "john"
- Inconsistent format: "New York", "new york", "NEW YORK"
Access String Methods
Use .str to access text functions:
code.py
import pandas as pd
df = pd.DataFrame({
'Name': [' john ', 'SARAH', 'Mike Smith']
})
# Use .str to access string methods
print(df['Name'].str.upper())Change Text Case
code.py
df = pd.DataFrame({
'Name': ['john doe', 'SARAH SMITH', 'Mike Jones']
})
# All uppercase
print(df['Name'].str.upper())
# JOHN DOE, SARAH SMITH, MIKE JONES
# All lowercase
print(df['Name'].str.lower())
# john doe, sarah smith, mike jones
# Title Case (First Letter Capital)
print(df['Name'].str.title())
# John Doe, Sarah Smith, Mike JonesRemove Extra Spaces
code.py
df = pd.DataFrame({
'Name': [' john ', ' sarah', 'mike ']
})
# Remove spaces from both sides
df['Name'] = df['Name'].str.strip()
print(df)Output:
Name
0 john
1 sarah
2 mike
Also:
- lstrip() - remove from left only
- rstrip() - remove from right only
Get Text Length
code.py
df = pd.DataFrame({
'Name': ['John', 'Sarah', 'Mike']
})
df['Name_Length'] = df['Name'].str.len()
print(df)Output:
Name Name_Length
0 John 4
1 Sarah 5
2 Mike 4
Replace Text
code.py
df = pd.DataFrame({
'Phone': ['123-456-7890', '987-654-3210']
})
# Remove dashes
df['Phone'] = df['Phone'].str.replace('-', '')
print(df)Output:
Phone
0 1234567890
1 9876543210
Check if Text Contains Something
code.py
df = pd.DataFrame({
'Email': ['john@gmail.com', 'sarah@yahoo.com', 'mike@gmail.com']
})
# Find Gmail users
gmail_users = df[df['Email'].str.contains('gmail')]
print(gmail_users)Output:
Email
0 john@gmail.com
2 mike@gmail.com
Common String Methods
| Method | What It Does |
|---|---|
| .str.upper() | ALL CAPS |
| .str.lower() | all lowercase |
| .str.title() | Title Case |
| .str.strip() | Remove spaces |
| .str.len() | Get length |
| .str.replace('a', 'b') | Replace text |
| .str.contains('x') | Check if contains |
Key Points
- Use .str before any text method
- strip() removes extra spaces
- upper(), lower(), title() change case
- replace() finds and replaces text
- contains() checks if text exists
What's Next?
Learn more string methods like split, extract, and starts/ends with.