AppDividend
Latest Code Tutorials

Pandas Series Value_counts Tutorial With Example

0

Pandas Series value_counts Tutorial With Example is today’s topic. Pandas value_counts() method returns an object containing counts of unique values in sorted order. The resulting object elements contain descending order so that the first element is the most frequently-occurring element. It excludes NA values by default.

Pandas Series value_counts Tutorial With Example

Let’s see the syntax for a 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.

Python Pandas 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 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 value_counts Tutorial With Example

 

Next step is to use a value_counts function on the any one of 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.

 

Python Pandas value_counts example

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

If you are familiar with SQL then you have 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 example

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

 

Pandas Series Value_counts Tutorial With Example Tutorial

Finally, Pandas Series Value_counts Tutorial With Example is over.

Leave A Reply

Your email address will not be published.

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