Python Forum
pandas pivot table: How to find count for each group in Index and Column
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
pandas pivot table: How to find count for each group in Index and Column
#1
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     |
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Get an average of the unique values of a column with group by condition and assign it klllmmm 0 220 Feb-17-2024, 05:53 PM
Last Post: klllmmm
  Python Alteryx QS-Passing pandas dataframe column inside SQL query where condition sanky1990 0 686 Dec-04-2023, 09:48 PM
Last Post: sanky1990
  find and group similar words with re? cartonics 4 681 Oct-27-2023, 05:36 PM
Last Post: deanhystad
  Trying to get counts/sum/percentages from pandas similar to pivot table cubangt 6 1,323 Oct-06-2023, 04:32 PM
Last Post: cubangt
  Using pyodbc&pandas to load a Table data to df tester_V 3 746 Sep-09-2023, 08:55 PM
Last Post: tester_V
  Find a string from a column of one table in another table visedwings049 8 1,084 Sep-07-2023, 03:22 PM
Last Post: deanhystad
  pandas : problem with conditional filling of a column Xigris 2 591 Jul-22-2023, 11:44 AM
Last Post: Xigris
  python pandas sql table with header mg24 3 1,854 Dec-08-2022, 08:31 PM
Last Post: Larz60+
  Need Help! Pandas EXCEL PIVOT psb3958 1 914 Nov-13-2022, 10:37 PM
Last Post: deanhystad
  Row Count and coloumn count Yegor123 4 1,262 Oct-18-2022, 03:52 AM
Last Post: Yegor123

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020