Cutting Memory Bloat in Large Pandas Tables with Categorical Dtype
Learn how converting repeated string columns to pandas Categorical dtype slashes memory use and speeds up group‑by operations.
19 May 2026, 13:47 UTC

Problem: Memory bloat from object dtype
A 10 GB CSV that contains many repeated string values often inflates memory usage because each column is stored as object dtype. This leads to high RAM consumption and slower I/O.
Thesis: Reduce memory and speed up operations with categorical dtype
Converting eligible object columns to pandas.Categorical dramatically shrinks the memory footprint and can accelerate group‑by and join operations while preserving the original data semantics.
When to convert
- The column has a limited set of unique values (typically fewer than a few thousand).
- All values are hashable (e.g., strings, numbers, dates).
- You have verified that unexpected strings will not appear after conversion.
Implementation steps
- Load the data, for example
df = pd.read_csv('large_file.csv'). Ensure you have read‑write permissions on the file. - Identify candidate columns such as
cityorcategory. - Convert the column:
df['city'] = df['city'].astype('category'). This operation requires no special permissions beyond accessing the DataFrame. - Verify the change with
print(df.dtypes)andprint(df.memory_usage(deep=True).sum()).
Worked example
import pandas as pd
# Sample data mimicking a large CSV
data = {
'city': ['New York']*1000000 + ['Los Angeles']*500000 + ['Chicago']*300000,
'category': ['A']*2000000 + ['B']*1000000 + ['C']*500000,
'value': range(1800000)
}
df = pd.DataFrame(data)
# Before conversion
print('Memory before:', df.memory_usage(deep=True).sum() / 1e9, 'GB')
print(df.dtypes)
# Convert eligible columns
for col in ['city', 'category']:
df[col] = df[col].astype('category')
# After conversion
print('Memory after:', df.memory_usage(deep=True).sum() / 1e9, 'GB')
print(df.dtypes)
# Compare groupby speed
%timeit df.groupby('city').sum() # object dtype
%timeit df.groupby('city').sum() # categorical dtype
Trade‑offs and limitations
- Memory savings come at the cost of slightly higher CPU for string manipulations; the
straccessor is unavailable on categorical columns and may require a temporary conversion. - When writing to CSV or other formats, categorical data is stored as plain strings, so you must re‑apply
astype('category')after loading if memory is critical. - Unexpected new strings become new categories, which can erode the memory benefit.
Verification checklist
- Run
df.memory_usage(deep=True).sum()before and after conversion; expect a 50‑80 % reduction in total memory usage. - Execute the same
groupbyoperation on the object‑dtype and categorical‑dtype versions and confirm that the results are identical.
Closing
When your dataset shows the right characteristics, converting repeated string columns to categorical dtype is a straightforward engineering decision that can cut memory usage dramatically and improve downstream analytics. Apply the conversion, verify the savings, and enjoy faster, more efficient pandas workflows.
0 replies
A thoughtful contribution can make all the difference. Be the first to share one.