Concursos
How can I count values in a column in pandas?
You can count values in a column in a pandas DataFrame using several methods, depending on what exactly you want to achieve.
✅ 1. Using value_counts()
(Most Common Method)
import pandas as pd
# Example DataFrame
df = pd.DataFrame({
'Fruit': ['Apple', 'Banana', 'Apple', 'Orange', 'Banana', 'Banana']
})
# Count occurrences of each unique value in the 'Fruit' column
count = df['Fruit'].value_counts()
print(count)
Output:
Banana 3
Apple 2
Orange 1
Name: Fruit, dtype: int64
- Sorted by default (descending order).
- Use
normalize=True
to get proportions instead of counts.
✅ 2. Using groupby()
with size()
# Group by unique values and count occurrences
count = df.groupby('Fruit').size()
print(count)
Output:
Fruit
Apple 2
Banana 3
Orange 1
dtype: int64
- Similar to
value_counts()
but useful when working with multiple columns.
✅ 3. Using count()
(Counts Non-Null Values Only)
# Count non-null values in the 'Fruit' column
non_null_count = df['Fruit'].count()
print(non_null_count)
Output:
6
- Note: This counts all non-null values, not unique occurrences.
✅ 4. Using collections.Counter
(For More Flexibility)
from collections import Counter
# Count occurrences using Counter
count = Counter(df['Fruit'])
print(count)
Output:
Counter({'Banana': 3, 'Apple': 2, 'Orange': 1})
- Useful if you need to perform additional operations with the counts.
📊 Summary:
value_counts()
→ Best for quick frequency counts of unique values.groupby().size()
→ Ideal when grouping by multiple columns.count()
→ Counts non-null values (not specific to unique occurrences).Counter
→ Flexible for advanced counting operations.