11.1 C
London
Sunday, July 7, 2024
HomePandas in PythonGeneral Functions in PythonPandas: How to Count Occurrences of Specific Value in Column

Pandas: How to Count Occurrences of Specific Value in Column

Related stories

Learn About Opening an Automobile Repair Shop in India

Starting a car repair shop is quite a good...

Unlocking the Power: Embracing the Benefits of Tax-Free Investing

  Unlocking the Power: Embracing the Benefits of Tax-Free Investing For...

Income Splitting in Canada for 2023

  Income Splitting in Canada for 2023 The federal government’s expanded...

Can I Deduct Home Office Expenses on my Tax Return 2023?

Can I Deduct Home Office Expenses on my Tax...

Canadian Tax – Personal Tax Deadline 2022

  Canadian Tax – Personal Tax Deadline 2022 Resources and Tools...

You can use the following syntax to count the occurrences of a specific value in a column of a pandas DataFrame:

df['column_name'].value_counts()[value]

Note that value can be either a number or a character.

The following examples show how to use this syntax in practice.

Example 1: Count Occurrences of String in Column

The following code shows how to count the number of occurrences of a specific string in a column of a pandas DataFrame:

import pandas as pd

#create DataFrame
df = pd.DataFrame({'team': ['A', 'A', 'B', 'B', 'B', 'B', 'C', 'C'],
                   'points': [25, 12, 15, 14, 19, 23, 25, 29],
                   'assists': [5, 7, 7, 9, 12, 9, 9, 4],
                   'rebounds': [11, 8, 10, 6, 6, 5, 9, 12]})

#count occurrences of the value 'B' in the 'team' column
df['team'].value_counts()['B']

4

From the output we can see that the string ‘B’ occurs 4 times in the ‘team’ column.

Note that we can also use the following syntax to find how frequently each unique value occurs in the ‘team’ column:

#count occurrences of every unique value in the 'team' column
df['team'].value_counts()

B    4
A    2
C    2
Name: team, dtype: int64

Example 2: Count Occurrences of Numeric Value in Column

The following code shows how to count the number of occurrences of a numeric value in a column of a pandas DataFrame:

import pandas as pd

#create DataFrame
df = pd.DataFrame({'team': ['A', 'A', 'B', 'B', 'B', 'B', 'C', 'C'],
                   'points': [25, 12, 15, 14, 19, 23, 25, 29],
                   'assists': [5, 7, 7, 9, 12, 9, 9, 4],
                   'rebounds': [11, 8, 10, 6, 6, 5, 9, 12]})

#count occurrences of the value 9 in the 'assists' column
df['assists'].value_counts()[9]

3

From the output we can see that the value 9 occurs 3 times in the ‘assists’ column.

We can also use the following syntax to find how frequently each unique value occurs in the ‘assists’ column:

#count occurrences of every unique value in the 'assists' column
df['assists'].value_counts()

9     3
7     2
5     1
12    1
4     1
Name: assists, dtype: int64

From the output we can see:

  • The value 9 occurs 3 times.
  • The value 7 occurs 2 times.
  • The value 5 occurs 1 time.

And so on.

Additional Resources

The following tutorials explain how to perform other common operations in pandas:

How to Count Unique Values in Pandas
How to Count Missing Values in a Pandas
How to Count Observations by Group in Pandas

Subscribe

- Never miss a story with notifications

- Gain full access to our premium content

- Browse free from up to 5 devices at once

Latest stories