Python Forum
pandas pivot table: How to find count for each group in Index and Column - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: pandas pivot table: How to find count for each group in Index and Column (/thread-35351.html)



pandas pivot table: How to find count for each group in Index and Column - JaneTan - Oct-23-2021

I am new to pivot table. I just want the count of each Age Grp by Mth. I tried

pivot1=pd.pivot_table(df,index=[ "Age Grp"], columns=["Mth"], values=["Age Grp"], aggfunc=pd.Series.nunique)
but get ValueError: Grouper for 'Age Grp' not 1-dimensional

DF

 |Age Grp | Mth  | 
 | 0-4       |  1   |  
 | 5 -9      | 5    |  
 | 0-4       | 10   | 
 | 10-14    | 5  |  
Desired Outcome


     Mth    |  1    | 5    | 10    |
 |Age Grp |                         |
 | 0-4       |  1    |   0  | 1     |
 | 5 -9      | 0     |   1  | 0     |
 | 10-14    |  0   |   1  | 0     |