Three columns A, B, and C are populated by 0's and 1's. I need the count summary but grouped by the same values within each column which is 0 and 1 using python. Thanks
Here's the sample data and output:
CodePudding user response:
Apply pd.value_counts
to generate a cross table of unique value counts:
import pandas as pd
df = pd.DataFrame({'A': [0,1]*7, 'B': [1,0,1,0,0,0,1,1,1,0,0,0,0,0], 'C': [0,1,0,1,0,1,0,0,0,1,1,1,0,0]})
df.apply(pd.value_counts)
# A B C
# 0 7 9 8
# 1 7 5 6
CodePudding user response:
If there are only 0/1 one option is to compute the sum of 1s and it's difference to the dataframe length:
pd.DataFrame([(s:=df.sum()), len(df)-s], index=[1,0])
Output:
A B C
1 7 5 6
0 7 9 8