-1

I am trying to count the number of True and False values in a pandas df like this. However, I'm having trouble first using groupby() to isolate my groups before using count().

Here's a toy example of my df in which A, B, and C are columns:

A   B   C
1   a   true
1   a   true
1   b   true
1   b   false
2   c   false
2   c   true
2   c   true
2   d   false

Does anyone have help? Here's my desired output:

B   true   false
a   2      0
b   1      1
c   1      2
d   0      1

I'm new to coding in python, so many thanks in advance!

psychcoder
  • 477
  • 2
  • 10
  • 1
    Does this answer your question? [How to pivot a dataframe](https://stackoverflow.com/questions/47152691/how-to-pivot-a-dataframe) – Umar.H Jul 09 '20 at 22:14

1 Answers1

2

We can try crosstab

pd.crosstab(df.B,df.C)
C  False  True 
B              
a      0      2
b      1      1
c      1      2
d      1      0
BENY
  • 296,997
  • 19
  • 147
  • 204