Count the frequency that a value occurs in a dataframe column
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.
Introduction
The primary way to count value frequencies in a pandas DataFrame column is df['column'].value_counts(), which returns a Series of unique values sorted by frequency (most common first). For relative frequencies, pass normalize=True to get proportions. For grouping and counting across multiple columns, use df.groupby('column').size() or df.groupby('column')['other'].count(). These methods cover virtually all frequency-counting scenarios in data analysis.
value_counts — The Standard Approach
Binning Continuous Data
value_counts can bin numeric data into ranges:
groupby for Multi-Column Counts
crosstab for Frequency Tables
Counting with Conditions
Performance Comparison
For a single column, value_counts() is fastest. For multi-column grouping, groupby().size() is the right tool.
Converting Results
Common Pitfalls
- Confusing
size()withcount()in groupby:size()counts all rows including NaN values, whilecount()excludes NaN. Usesize()when you want the total number of rows per group, andcount()when you want non-null entries for a specific column. - Forgetting
dropna=Falsefor NaN counting: By default,value_counts()excludes NaN values. If your analysis depends on knowing how many missing values exist, passdropna=Falseexplicitly. - Using
len(df.groupby('col'))instead ofnunique():len(df.groupby('col'))returns the number of unique groups (same asnunique()), butnunique()is more readable and works directly on a Series without groupby overhead. - Expecting
value_counts()on the entire DataFrame:value_counts()works on a Series (single column). Callingdf.value_counts()counts unique row combinations across all columns, which is rarely what you want. Always specify the column. - Not resetting the index after groupby counts:
groupby().size()returns a Series with a MultiIndex. To use the result as a regular DataFrame (for merging, exporting, or plotting), call.reset_index(name='count').
Summary
- Use
df['col'].value_counts()for single-column frequency counts (fastest, most common) - Pass
normalize=Truefor proportions orbins=Nfor numeric binning - Use
df.groupby(['col1', 'col2']).size()for multi-column frequency counts - Use
pd.crosstab()for two-way frequency tables with optional margins and normalization - Call
.reset_index(name='count')to convert grouped results back to a regular DataFrame
Related reading
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.