I need a total column next to my side index (Center_Name). There's margins function in crosstab, but it is limited to one column only . I have the following code to have multiple columns in a crosstab, but unable to add a total column.
The code I'm using(Deriving data from Excel):
cols=['Gender','QAge_Post']
q2=(nb.melt(id_vars='Center_Name',value_vars=cols)
.groupby([pd.Grouper(key='Center_Name'),'value'])
.size()
.unstack(fill_value=0))
q2
I have 50 rows with columns Center_Name, Gender, QAge_Post. I'm using Center_name as index and the remaining two as columns.
The output:
Center_Name | 18 - 25 Years | 26 - 35 years | Male | Female |
---|---|---|---|---|
Delhi | 8 | 5 | 3 | 10 |
Kolkata | 2 | 6 | 4 | 4 |
Lucknow | 7 | 5 | 5 | 7 |
Mumbai | 0 | 11 | 2 | 9 |
Pune | 3 | 3 | 5 | 1 |
I need a total row in the end as well