How to Count the Number of Unique Values of a Column of a Pandas Dataframe Object in Python


In this article, we show how to count the number of unique values of a pandas dataframe object in Python.

What this means is that we count the number of each unique values that appear within a certain column of a pandas dataframe.

Let's say, for example, we have a table for restaurant dinners that people eat. One of the columns is labeled 'day'. We can check which days of the week people attend this restaurant and get a value count of how many people eat at this restaurant for each day using the value_counts() function.

The value_counts() function allows us to get a breakdown of all unique values of a column and shows the quantitative analysis of each unique value. For example, with our previous, it will show how many people ate on Sunday, how many people ate on Friday, how many people ate at the restaurant on Saturday (for each day that exists).

Inside of this value_counts() function, you place the name of the column that you want the value breakdown of.

So, as an example, I will use the tips pandas dataframe object. This contains the columns: total_bill, tip, sex, smoker, day, time, and size.

To find the value breakdown of the 'day' column, the following code is used shown below.

So let's go over the code now.

So the first thing is we import seaborn, because we want to use the tips datasheet that seaborn provides.

We then load in the tips data using the sns.load_dataset('tips'). We set this equal to the variable, df, standing for dataframe.

We then take this df variable and we use the value_counts() function in order to get the unique value breakdown of the 'day' column. You can see that Saturday is the greatest day that most people ate at the restaurant with a total of 87 people eating there on that day. This is followed by Sunday being the next greatest day with a count of 76. Then Thursday 62 people ate at the restaurant. Lastly, Friday 19 people ate at the restaurant.

This is very useful quantitative breakdown of columns that pandas does with a single function, value_counts(). This is especially useful for large datasets where calculating this manually is extremely laborious and time-intensive.

As another example, we will get the quantitative breakdown of the 'sex' column.

This is shown in the following code below.

So we've gotten the quantitative breakdown of the 'sex' column.

You can see that males totally dominate over females in the restaurant. 157 men ate at the restaurant, while 87 women ate there.

So the value_counts() function is a powerful function in pandas that allows us to get quantitative analysis of any column of a dataframe in Python.

Related Resources

How to Randomly Select From or Shuffle a List in Python

HTML Comment Box is loading comments...