NealWalters
NealWalters

Reputation: 18197

Pandas - get count of each boolean field

I have other programs where I group and count fields. Now, I want to get a count of each boolean field. Is there a Pandas way to do that rather than me looping and writing my own code? Ideally, I would generated a new dataframe with the results (kind of like what I did here).

Easy Example CSV Data (data about poker hands generated):

Hand,Other1,Other2,IsFourOfAKind,IsThreeOfAKind,IsPair 
1,'a','b',1,0,0
2,'c','d',0,1,0
3,'a','b',0,1,0
4,'x','y',0,0,1
5,'a','b',0,0,1
6,'a','b',0,0,1
7,'a','b',0,0,1 

Program:

import pandas as pd
import warnings 
filename = "./data/TestGroup2.csv"

# tell run time to ignore certain read_csv type errors (from pandas)
warnings.filterwarnings('ignore', message="^Columns.*")

count_cols = ['IsFourOfAKind','IsThreeOfAKind','IsPair ']
enter code here
#TODO - use the above to get counts of only these columns 

df = pd.read_csv(filename)
print(df.head(10))

Desired Output - could just be a new dataframe

 Column           Count 
IsFourOfAKind      1
IsThreeOfAKind     2
IsPair             3

Upvotes: 0

Views: 70

Answers (1)

wwnde
wwnde

Reputation: 26676

Please try:

df.filter(like='Is').sum(0)

or did you need;

df1=df.filter(like='Is').agg('sum').reset_index().rename(columns={'index':'column', 0:'count'})

Upvotes: 2

Related Questions