ADVERTISEMENT
Google AdSense Slot (headerLeaderboard)

Set your Publisher ID in src/config/ads.ts to display live ads.

Pandas Tutorial• 6 min read

String Methods (.str) & Memory-Efficient Categoricals

Vectorized text manipulation, regex extraction, and categorical data types

Pandas provides the .str accessor for vectorized string operations and the category dtype to compress repetitive string columns by up to 90%.

Vectorized String Processing & Categories

The .str accessor allows vectorized operations: .str.lower(), .str.strip(), .str.contains(), .str.replace(), and .str.extract(r"pattern").

The category dtype: Instead of storing repetitive string objects across millions of rows (like "CA", "NY", "TX"), a category stores an integer lookup table, reducing memory consumption by over 80% and speeding up groupby operations.

Vectorized String Cleaning & Category Conversion
import pandas as pd

df = pd.DataFrame({
    "raw_code": ["  PROD-101-USA  ", "prod-204-gbr ", "  PROD-305-usa"],
    "country": ["USA", "GBR", "USA"]
})

# Strip whitespace and uppercase
df["clean_code"] = df["raw_code"].str.strip().str.upper()

# Convert country to memory-efficient category dtype
df["country"] = df["country"].astype("category")

print("Cleaned DataFrame:
", df)
print("
Memory usage with category:
", df.memory_usage(deep=True))
Categoricals store strings as lightweight integer code pointers.
ADVERTISEMENT
Google AdSense Slot (inContentBanner)

Set your Publisher ID in src/config/ads.ts to display live ads.

Test Yourself With Exercise

+15 XP

Why should a column containing repetitive strings (such as country codes or state names) be converted to the `category` data type?