I'm using python 3 and pandas. I have this pivot table below.
>>> print(p.head())
question_id 1 2 3 4 ... 26 27 28 29
assessment_attempt_id
...
243908 21-24 Female 4th year undergraduate White ... Disagree Disagree Agree Agree
290934 25-29 Male Prefer Not to Answer Black or African American ... Neutral Neutral Neutral Neutral
312457 18-20 Female 1st year undergraduate White ... Strongly Agree Strongly Agree Strongly Agree Strongly Agree
312766 18-20 Female 2nd year undergraduate Hispanic or Latina/o ... Agree Agree Agree Agree
312786 21-24 Female 4th year undergraduate Black or African American ... Strongly Disagree Agree Agree Agree
It is produced from this command:
p= pandas.pivot_table(df, index=["assessment_attempt_id"], columns=["question_id"], values="text", aggfunc='first')
The table is basically exactly what I want. Now I just want columns 1,2,3,4 and the assessment_attempt_id column in a Datatable, so I can join that data by assessment_attempt_id with another existing data table.
Normally I would subset the data by doing something like this:
df1 = df[['a','b']]
but that produces and error: KeyError: "['1' '2'] not in index" It seems like this should be a simple and solved problem but I can not find the answer. I also tried a groupby variation which produced the same output, and also I could not extract the columns I wanted. I assume I can't at the multi-index, but I don't know how. Thank you.