Table of Contents
To count the values in a specific column in Pandas based on a certain condition, one can use the “.loc” function to filter out the desired rows and then use the “.count” function to calculate the number of values in that column. This approach allows for the counting of specific values within a column rather than the total number of rows.
Pandas: Count Values in Column with Condition
You can use the following methods to count the number of values in a pandas DataFrame column with a specific condition:
Method 1: Count Values in One Column with Condition
len(df[df['col1']=='value1'])
Method 2: Count Values in Multiple Columns with Conditions
len(df[(df['col1']=='value1') & (df['col2']=='value2')])
The following examples show how to use each method in practice with the following pandas DataFrame:
import pandas as pd
#create DataFrame
df = pd.DataFrame({'team': ['A', 'A', 'A', 'A', 'B', 'B', 'B', 'B'],
'pos': ['Gu', 'Fo', 'Fo', 'Fo', 'Gu', 'Gu', 'Fo', 'Fo'],
'points': [18, 22, 19, 14, 14, 11, 20, 28]})
#view DataFrame
print(df)
team pos points
0 A Gu 18
1 A Fo 22
2 A Fo 19
3 A Fo 14
4 B Gu 14
5 B Gu 11
6 B Fo 20
7 B Fo 28Example 1: Count Values in One Column with Condition
The following code shows how to count the number of values in the team column where the value is equal to ‘A’:
#count number of values in team column where value is equal to 'A' len(df[df['team']=='A']) 4
We can see that there are 4 values in the team column where the value is equal to ‘A.’
Example 2: Count Values in Multiple Columns with Conditions
The following code shows how to count the number of rows in the DataFrame where the team column is equal to ‘B’ and the pos column is equal to ‘Gu’:
#count rows where team is 'B' and pos is 'Gu' len(df[(df['team']=='B') & (df['pos']=='Gu')]) 2
We can see that there are 2 rows in the DataFrame that meet both of these conditions.
We can use similar syntax to count the number of rows that meet any number of conditions we’d like.
For example, the following code shows how to count the number of rows that meet three conditions:
- team is equal to ‘B’
- pos is equal to ‘Gu’
- points is greater than 12
#count rows where team is 'B' and pos is 'Gu' and points > 15 len(df[(df['team']=='B') & (df['pos']=='Gu') & (df['points']>12)]) 1
We can see that only 1 row in the DataFrame meets all three of these conditions.
Additional Resources
The following tutorials explain how to perform other common tasks in pandas:
Cite this article
stats writer (2024). How can I count the values in a specific column in Pandas based on a condition?. PSYCHOLOGICAL SCALES. Retrieved from https://scales.arabpsychology.com/stats/how-can-i-count-the-values-in-a-specific-column-in-pandas-based-on-a-condition/
stats writer. "How can I count the values in a specific column in Pandas based on a condition?." PSYCHOLOGICAL SCALES, 28 Jun. 2024, https://scales.arabpsychology.com/stats/how-can-i-count-the-values-in-a-specific-column-in-pandas-based-on-a-condition/.
stats writer. "How can I count the values in a specific column in Pandas based on a condition?." PSYCHOLOGICAL SCALES, 2024. https://scales.arabpsychology.com/stats/how-can-i-count-the-values-in-a-specific-column-in-pandas-based-on-a-condition/.
stats writer (2024) 'How can I count the values in a specific column in Pandas based on a condition?', PSYCHOLOGICAL SCALES. Available at: https://scales.arabpsychology.com/stats/how-can-i-count-the-values-in-a-specific-column-in-pandas-based-on-a-condition/.
[1] stats writer, "How can I count the values in a specific column in Pandas based on a condition?," PSYCHOLOGICAL SCALES, vol. X, no. Y, ص Z-Z, June, 2024.
stats writer. How can I count the values in a specific column in Pandas based on a condition?. PSYCHOLOGICAL SCALES. 2024;vol(issue):pages.
