Lisle
Lisle

Reputation: 1690

Obtaining Counts by Column Pandas

I have a df with several boolean columns, here is an excerpt:

 L1 MATCH  L2 MATCH  L3 MATCH  L4 MATCH  L5 MATCH  
0  True      True      True      False    False  
1  True      True      False     False    False  
2  True      True      True      True     False  
3  True      False     True      True     False  
4  True      True      False     True     False  

I would like to obtain the counts of True vs False for each of those columns as follows (or similar). If easier, I would take the transpose of the below as well.

             True       False
L1 MATCH     12345      6789
L2 MATCH     12345      6789
L3 MATCH     12345      6789
L4 MATCH     12345      6789
L5 MATCH     12345      6789

Upvotes: 2

Views: 53

Answers (1)

Scott Boston
Scott Boston

Reputation: 153460

Let's use pd.concat and sum:

pd.concat([df.sum(),(~df).sum()], axis=1, keys=['True','False'])

Output:

          True  False
L1 MATCH     5      0
L2 MATCH     4      1
L3 MATCH     3      2
L4 MATCH     3      2
L5 MATCH     0      5

Upvotes: 2

Related Questions