0

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
NealWalters
  • 17,197
  • 42
  • 141
  • 251

1 Answers1

2

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'})
wwnde
  • 26,119
  • 6
  • 18
  • 32
  • 1
    Second one - Wow, that is so cool. I have many years of SQL, but so powerful what you can do with one command in Pandas! I just like second, in case I need more control to loop, match to expected values, etc... – NealWalters Feb 25 '21 at 02:23