I am working on a survey and the data looks like this:
ID Q1 Q2 Q3 Gender Age Dep Ethnicity
001 Y N Y F 22 IT W
002 N Y Y M 35 HR W
003 Y N N F 20 IT A
004 Y N Y M 54 OPRE B
005 Y N Y M 42 OPRE B
Now, I'd like to add two indexes Dep and Gender to create a table like:
Question Dep Response ID % response
Q1 IT Y 2 100
IT N 0 0
HR Y 0 0
HR N 1 100
OPRE Y 2 100
OPRE N 0 0
Q2 IT Y 0 0
IT N 2 100
HR Y 1 100
HR N 0 0
OPRE Y 0 0
OPRE N 2 100
Q3 ......
My codes are like this:
df['% response'] = df['ID']/df['ID'].sum()
Which gives me
Question Dep Response ID % response
Q1 IT Y 2 20
IT N 0 0
HR Y 0 0
HR N 1 10
OPRE Y 2 20
OPRE N 0 0
Q2 IT Y 0 0
IT N 2 20
HR Y 1 10
HR N 0 0
OPRE Y 0 0
OPRE N 2 20
Q3 ......
I think the denominator is wrong. It should be grouped by the question and Dep and then do the count instead of sum all the IDs. Does anyone can help?