How can I count the occurrences of a specific value in a column using Pandas?

How can I count the occurrences of a specific value in a column using Pandas?

Pandas is a popular data analysis library in Python that provides powerful tools for manipulating and analyzing data. One useful feature of Pandas is the ability to count the occurrences of a specific value in a column. This can be achieved by using the “value_counts()” function, which returns a series containing the frequency of each unique value in the column. This function can be applied to a specific column in a Pandas dataframe, allowing for efficient and accurate counting of occurrences. This feature is particularly useful for data analysis and can help in identifying patterns and trends within a dataset.

Pandas: Count Occurrences of Specific Value in Column


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.

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

Cite this article

stats writer (2024). How can I count the occurrences of a specific value in a column using Pandas?. PSYCHOLOGICAL SCALES. Retrieved from https://scales.arabpsychology.com/stats/how-can-i-count-the-occurrences-of-a-specific-value-in-a-column-using-pandas/

stats writer. "How can I count the occurrences of a specific value in a column using Pandas?." PSYCHOLOGICAL SCALES, 12 May. 2024, https://scales.arabpsychology.com/stats/how-can-i-count-the-occurrences-of-a-specific-value-in-a-column-using-pandas/.

stats writer. "How can I count the occurrences of a specific value in a column using Pandas?." PSYCHOLOGICAL SCALES, 2024. https://scales.arabpsychology.com/stats/how-can-i-count-the-occurrences-of-a-specific-value-in-a-column-using-pandas/.

stats writer (2024) 'How can I count the occurrences of a specific value in a column using Pandas?', PSYCHOLOGICAL SCALES. Available at: https://scales.arabpsychology.com/stats/how-can-i-count-the-occurrences-of-a-specific-value-in-a-column-using-pandas/.

[1] stats writer, "How can I count the occurrences of a specific value in a column using Pandas?," PSYCHOLOGICAL SCALES, vol. X, no. Y, ص Z-Z, May, 2024.

stats writer. How can I count the occurrences of a specific value in a column using Pandas?. PSYCHOLOGICAL SCALES. 2024;vol(issue):pages.

Download Post (.PDF)
Slide Up
x
PDF
Scroll to Top