I have a dataframe which looks like :
col1 col2 col3 col4
0 fst 4 7
0 snd 5 8
1 fst 6 9
I apply:
pd.pivot_table(df, index='col1', columns='col2', aggfunc='count', fill_value=0.0)
output:
col3 col4
fst snd fst snd
index
0 1 1 1 1
1 1 0 1 0
Expected output:
fst snd
index
0 1 1
1 1 0
Why columns have multiindex?
CodePudding user response:
You need to add the values
out = pd.pivot_table(df, index='col1', columns='col2', values = 'col3',aggfunc='count', fill_value=0.0)
Also in your case ,do pd.crosstab
out = pd.crosstab(df.col1, df.col2)