Pandas value_counts method Implementation in Python

Pandas value_counts method Implementation in Python

There are many inbuilt functions in the pandas python library that you can easily manipulate dataset easily. Pandas value_counts is one of them. It allows you to find the counts of unique values in a series and dataframe columns. In this entire tutorial, you will know how to implement pandas value_counts with examples.

Examples of Pandas value_counts

In this section, you will know the various examples of pandas value_counts. Please note that  I am demonstrating the code on Python Jupyter notebook, so for better understanding compile code on it.

Example 1: Finding unique counts in Series

Suppose you have a Series and want to know the occurrences of unique values for each element. Then you will use the function pandas.Series.value_counts().

Execute the below lines of code.

import pandas as pd
series = pd.Series([10, 20, 10, 20, 30, 40])
print(series.value_counts())

Output

Finding unique counts in Series
Finding unique counts in Series

Example 2: Find  the unique count on Index

Let say I have index values of a dataframe. Then you can find the number of times index value comes in it using value_counts() method. Just execute the code and see the output.

import pandas as pd
series = pd.Index([10, 20, 10, 20, 30, 40])
print(series.value_counts())

Output

Find  the unique count on Index
Find  the unique count on Index

Example 3: Counting the NaN values

If your dataset contains NaN values then also pandas value_counts() can find the occurrence if you use the dropna=False argument. Firstly I will create a Sample Series with NaN and then apply value_counts() on it.

import pandas as pd
import numpy as np
series = pd.Series([10, 20, 10, 20, 30, np.nan])
print(series.value_counts(dropna=False))

Output

Counting the NaN values
Counting the NaN values

Example 4:  Applying value_counts on dataframe

The above examples were for series. In this example, you will know the implementation of pandas value_counts on dataframe. But before that let’s first create a Sample dataframe.  After that, you will use the value_counts() method on datataframe columns.

import pandas as pd
df = pd.DataFrame({"col1":[10, 20, 10, 20, 30, 40],"col2":[1,2,3,1,2,4]},index=["A","B","C","D","E","F"])
print(df)

Output

Sample dataframe for value_counts method
Sample dataframe for value_counts method

Now let’s apply the value_counts on dataframe.

import pandas as pd
df = pd.DataFrame({"col1":[10, 20, 10, 20, 30, 40],"col2":[1,2,3,1,2,4]},index=["A","B","C","D","E","F"])
print(df["col1"].value_counts())
print(df["col2"].value_counts())
Using Value_counts on dataframe
Using Value_counts on dataframe

The above part does not have NaN values. Suppose you have NaN value in it and if you pass the dropna=True as an argument inside the value_counts() function then it will remove the records for all the rows containing NaN. Just Execute the below lines of code and see the output.

import pandas as pd
import numpy as np
df = pd.DataFrame({"col1":[10, 20, 10, 20, np.nan, np.nan],"col2":[1,2,3,1,2,4]},
index=["A","B","C","D","E","F"])
print(df["col1"].value_counts(dropna=False))
print(df["col2"].value_counts())

Output

Using Value_counts on dataframe with NaN values
Using Value_counts on dataframe with NaN values

You can also sort the counts bypassing the ascending = True as an argument in the function.

import pandas as pd
import numpy as np
df = pd.DataFrame({"col1":[10, 20, 10, 20, np.nan, np.nan],"col2":[1,2,3,1,2,4]},
index=["A","B","C","D","E","F"])
print(df["col1"].value_counts(dropna=False))
print(df["col2"].value_counts(ascending=True))

Output

sorting the counts by passing the ascending argument
sorting the counts by passing the ascending argument

Conclusion

The value_counts method is mostly used to find the occurrence of the unique elements in the series or dataframe. These are the examples I have aggregated for the implementation of Pandas value_counts. I hope you have liked this tutorial. If you have any queries then you can contact us for more help.

Source:

Pandas Documentation

 

Join our list

Subscribe to our mailing list and get interesting stuff and updates to your email inbox.

Thank you for signup. A Confirmation Email has been sent to your Email Address.

Something went wrong.

Meet Sukesh ( Chief Editor ), a passionate and skilled Python programmer with a deep fascination for data science, NumPy, and Pandas. His journey in the world of coding began as a curious explorer and has evolved into a seasoned data enthusiast.
 
Thank you For sharing.We appreciate your support. Don't Forget to LIKE and FOLLOW our SITE to keep UPDATED with Data Science Learner