AppDividend
Latest Code Tutorials

Pandas value_counts Example | pandas.Series.value_counts

0

Pandas value_counts is an inbuilt pandas function that returns an object containing counts of unique values in sorted order. The resulting object elements include descending order so that the first element is the most frequently-occurring element.

Pandas series is a One-dimensional ndarray with axis labels. The labels need not be unique but must be a hashable type.

The object supports both integer- and label-based indexing and provides a host of methods for performing operations involving the index.

Pandas value_counts example

Content Overview

Pandas value_counts() function returns the Series containing counts of unique values.

The resulting object will be in descending order so that the first element is the most frequently-occurring element.

It excludes NA values by default.

Let’s see the syntax for the value_counts() method in Python Pandas Library.

Series.value_counts(normalize=False, sort=True, ascending=False, bins=None, dropna=True)

All the parameters are optional.

normalize: boolean, default False

If True, then the object returned will contain the relative frequencies of the unique values.

sort: boolean, default True

Sort by values

ascending: boolean, default False

Sort in ascending order

bins: integer, optional

Rather than count values, group them into half-open bins, a convenience for pd.cut only works with numeric data

dropna: boolean, default True

Don’t include counts of NaN.

Pandas Series value_counts example

Okay, now for this tutorial, we will use the Jupyter Notebook. Also, we need data to work on this project. You can save the CSV file from the below URL.

https://docs.google.com/spreadsheets/d/1zeeZQzFoHE2j_ZrqDkVJK9eF7OH1yvg75c8S-aBcxaU/edit#gid=0

Now, open the Jupyter Notebook and import the Pandas Library first.

Write the following code inside the first cell in Jupyter Notebook.

import pandas as pd

Run the cell by hitting Ctrl + Enter.

Okay, now we will use the  read_csv()function of the DataFrame data structure in Pandas. So write the following code in the next cell.

data = pd.read_csv('data.csv', skiprows=4)
data

So, we have used the read_csv() function and skipped the first four rows and then display the remaining rows. Run the cell and see the output.

Pandas Series Example

The next step is to use a value_counts function on any one of the columns.

Remember, the value_counts() function applies to Series, and selecting any column from the DataFrame becomes the series; that is why we can apply the value_counts() function to the columns.

Let’s count how many times each city has appeared in the dataset.

Write the following code in the next cell.

data.City.value_counts()

Here, the data is the Dataframe, and we have accessed its column called City.

That means now, it has become a Series, and then we have applied the value_counts method.

Run the cell and see the output.

Series value_counts

See, the output data is by default sorted from high volume to low volume. 

If you are familiar with SQL, then you might use the query to output this kind of results from the database tables.

Let’s see another example.

Write the following code in the next cell.

data.Sport.value_counts()

Run the cell and see the output.

Python Pandas Series Value_counts Tutorial With Example

In the above screenshot, some of them are not fitted, but you can see all the values in the output.

You can also pass the optional parameter like sort=False.

Write the following code in a cell.

data.Sport.value_counts(sort=False)

It will give you the unsorted output values.

pandas.Series.value_counts

If you are not coding this on Jupyter Notebook and use the editor like Visual Studio Code, then you can write the following code inside the app.py file and see the output inside the terminal.

// app.py

import pandas as pd
data = pd.read_csv('data.csv', skiprows=4)
print(data)

print(data.City.value_counts())
print(data.Sport.value_counts())
print(data.Sport.value_counts(sort=False))

Now, run the file inside the terminal and see the output.

python3 app.py

Series Value_counts

#Extract values in Pandas value_counts()

Let’s say we have used pandas dataframe

.value_counts() which outputs:

 apple   5 
 sausage 2
 banana  2
 cheese  1

How do you extract the values from this in the order shown above e.g. max to min?

[apple, sausage, banana, cheese].

You can do the following operations.

dataframe
.value_counts().index.tolist()

Or try this.

dataframe
.value_counts().to_frame()

Finally, Pandas value_counts Example | pandas.Series.value_counts is over.

Recommended Posts

Pandas DataFrame.set_index() Tutorial

Pandas Boolean Indexing Example

Pandas Series Sort_values Tutorial

Pandas DataFrame read_csv Example

Leave A Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.