1

Does anybody have an idea how to count all boolean values(including the false one) in pivot table?

passed_exam is a column of boolean values.

This code performs the task only for true values which is great:

table = pd.pivot_table(df,index=["student","semester"], values=["passed_exam"],aggfunc=np.sum)

But I also want a column that counts all boolean values.

Thank you in advance!

Sheron
  • 585
  • 2
  • 9
  • 24

1 Answers1

2

I think you need groupby with size, last reshape by unstack:

df = pd.DataFrame({'student':['a'] * 4 + ['b'] * 6,
                   'semester':[1,1,2,2,1,1,2,2,2,2],
                   'passed_exam':[True, False] * 5})

print (df)
  passed_exam  semester student
0        True         1       a
1       False         1       a
2        True         2       a
3       False         2       a
4        True         1       b
5       False         1       b
6        True         2       b
7       False         2       b
8        True         2       b
9       False         2       b

table = df.groupby(["student","semester","passed_exam"])
          .size()
          .unstack(fill_value=0)
          .rename_axis(None, axis=1)
          .reset_index()
print (table)
  student  semester  False  True
0       a         1      1     1
1       a         2      1     1
2       b         1      1     1
3       b         2      2     2
jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252
  • Thanks a lot! Is it possible not to repeat a and b? I mean to have a in the first row and then blank in the second row? and always getting a dataframe as a result – Sheron Mar 09 '17 at 12:42
  • Sure, can you create new question with sample data, desired output and what you try? Thank you. – jezrael Mar 09 '17 at 12:44
  • And use my sample, no problem, or you can change it as you need. Maybe help [how to provide a great pandas example](http://stackoverflow.com/questions/20109391/how-to-make-good-reproducible-pandas-examples). – jezrael Mar 09 '17 at 12:46