Set your Publisher ID in src/config/ads.ts to display live ads.
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.
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))Set your Publisher ID in src/config/ads.ts to display live ads.
Test Yourself With Exercise
Why should a column containing repetitive strings (such as country codes or state names) be converted to the `category` data type?