Pandas Tutorial
Creating Objects
Viewing Data
Selection
Manipulating Data
Grouping Data
Merging, Joining and Concatenating
Working with Date and Time
Working With Text Data
Working with CSV and Excel files
Operations
Visualization
Applications and Projects
In pandas, the value_counts()
method is very useful when you want to count the unique values in a Series. It returns a Series containing counts of unique values, sorted in descending order.
Let's delve into a tutorial on how to use the Index.value_counts()
in pandas:
First, ensure you have pandas installed:
pip install pandas
import pandas as pd
data = pd.Series(['apple', 'banana', 'apple', 'orange', 'banana', 'apple']) print(data)
Output:
0 apple 1 banana 2 apple 3 orange 4 banana 5 apple dtype: object
value_counts()
on Series:count = data.value_counts() print(count)
Output:
apple 3 banana 2 orange 1 dtype: int64
From the above, we can see that 'apple' appeared 3 times, 'banana' appeared 2 times, and 'orange' appeared once.
value_counts()
on DataFrame's Index:If you have a DataFrame and want to count unique values in its index, you can first convert the index to a Series and then use value_counts()
.
df = pd.DataFrame({ 'A': [1, 2, 3, 4], 'B': [5, 6, 7, 8] }, index=['x', 'y', 'x', 'z']) index_counts = pd.Series(df.index).value_counts() print(index_counts)
Output:
x 2 z 1 y 1 dtype: int64
Normalize: If you want the relative frequencies instead of counts, use the normalize
argument:
print(data.value_counts(normalize=True))
This will give the proportion of each unique value in the Series.
Sort: By default, the output is sorted by counts. If you don't want to sort:
print(data.value_counts(sort=False))
The value_counts()
method in pandas is a powerful and convenient way to quickly understand the distribution of categorical data or the uniqueness of values in a Series or a DataFrame's index. Whether you're performing exploratory data analysis or cleaning up your data, value_counts()
is a tool you'll find yourself using often.
Count unique values in Pandas Index:
nunique()
method to count the number of unique values in an Index.unique_count = df['column_name'].index.nunique()
Using value_counts() on Pandas Index:
value_counts()
to get a Series containing counts of unique values in an Index.value_counts_series = df['column_name'].index.value_counts()
Find frequencies of unique values in Pandas Index:
value_counts()
.frequencies = df['column_name'].index.value_counts()
Count occurrences of each value in Pandas Index:
occurrences = df['column_name'].index.value_counts().reset_index()
Getting value counts of unique elements in Index:
value_counts()
on the Index to obtain counts of unique elements.counts = df.index.value_counts()
Pandas Index value_counts examples:
value_counts()
on a Pandas Index.value_counts = df.index.value_counts()
Displaying unique value frequencies in Pandas:
unique_frequencies = df.index.value_counts().reset_index()
Counting occurrences of each label in Pandas Index:
label_counts = df.index.value_counts().reset_index()
Analyzing value distribution in Pandas Index using value_counts():
value_counts()
.distribution_analysis = df.index.value_counts(normalize=True)