I get an unexpected error message when trying to slice two levels of a multiIndex...Any help on how to do it? Please look at the attached code...
I am running Python 3.7.1 and Pandas 0.23.4
I have this dataframe:
import pandas as pd
import itertools
index = list(itertools.product(['Ada','Quinn','Violet','Juan'],['Physics',
'Chemistry','Math','English']))
headr = list(itertools.product(['Exams','Labs', 'Particip'],
['I','II','III','IV']))
indx = pd.MultiIndex.from_tuples(index,names=['Student','Course'])
cols = pd.MultiIndex.from_tuples(headr) #Notice these are un-named
data = [[70+x+y+(x*y)%3 for x in range(12)] for y in range(16)]
df = pd.DataFrame(data,indx,cols)
dfls=df.sort_index(level=0);dfls
As you can see below, I can slice into one level of the dataframe without problems:
dfls.loc[(('Ada','Quinn'),('Math','Chemistry')),('Labs',('I','IV'))]
getting:
Labs
I IV
Student Course
Ada Chemistry 76 79
Math 78 81
Quinn Chemistry 81 84
Math 80 83
But when I try with two different levels:
dfls.loc[(('Ada','Quinn'),('Math','Chemistry')),[('Exams',('I','III')),
('Labs',('II','IV'))]]
I get the following error message:
ValueError: setting an array element with a sequence
How may I avoid this error message and get the result I am looking for? Thanking you in advance...