Question
I have a similar question to: Crosstab with multiple items but am not trying to do it in R, I am trying to do it in Python Pandas with Crosstab.
I have been trying to use Python Pandas crosstab function to make a demographic table, but have only been able to do one demographic at a time. In other words, I would like to make a crosstabs that has all row variables on the same level. Perhaps this is not the function of crosstabs and something like Pandas pivot table would be better for this?
Currently I use the following three lines of code, but would think there is someway to combine these:
genderTable = pd.crosstab(refQtrData['GENDER'], [refQtrData['FUNDINGSOURCE'],refQtrData['PROVIDER'],refQtrData['LOCATION']], margins='true')
raceTable = pd.crosstab(refQtrData['RACETH4'], [refQtrData['FUNDINGSOURCE'],refQtrData['PROVIDER'],refQtrData['LOCATION']], margins='true')
ageTable = pd.crosstab(refQtrData['REFERRED'], [refQtrData['FUNDINGSOURCE'],refQtrData['PROVIDER'],refQtrData['LOCATION']], values=refQtrData['AGEREF'], aggfunc='mean')
What I would like to do: Demographic Table
Other miscellaneous info
This is originally done in SPSS with the code below, but I am trying to move it over to python. In the same way that SPSS CTABLES allows me to have multiple categories and variables, I'd like to have multiple rows that correspond to different variables without them being on different levels.
CTABLES
/VLABELS VARIABLES= GENDER RACE AGE FUNDINGSOURCE PROVIDER LOCATION
DISPLAY=LABEL
/TABLE REFERRED [C][COUNT F40.0] + GENDER [C][COUNT F40.0, COLPCT.COUNT PCTPAREN40.0] + RACE
[C][COUNT F40.0, COLPCT.COUNT PCTPAREN40.0] + AGE [S][MEAN] + AGE [S][MINIMUM, MAXIMUM]
BY FUNDINGSOURCE [C] > PROVIDER [C] > LOCATION [C]
/SLABELS VISIBLE=NO
/CATEGORIES VARIABLES=GENDER RACE ORDER=A KEY=VALUE MISSING=INCLUDE EMPTY=INCLUDE
/CATEGORIES VARIABLES=FUNDINGSOURCE ORDER=A KEY=VALUE MISSING=INCLUDE EMPTY=EXCLUDE
/CATEGORIES VARIABLES=PROVIDER [1, 2] EMPTY=EXCLUDE
/CATEGORIES VARIABLES=LOCATION [1, 2] EMPTY=EXCLUDE.