I am trying to select individual rows from a multiindex dataframe using a list of multiindices.
For example. I have got the following dataframe:
Col1
A B C
1 1 1 -0.148593
2 2.043589
2 3 -1.696572
4 -0.249049
2 1 5 2.012294
6 -1.756410
2 7 0.476035
8 -0.531612
I would like to select all 'C' with (A,B) = [(1,1), (2,2)]
Col1
A B C
1 1 1 -0.148593
2 2.043589
2 2 7 0.476035
8 -0.531612
My flawed code for this is as follows:
import pandas as pd
import numpy as np
arrays = [np.array([1, 1, 1, 1, 2, 2, 2, 2]), np.array([1, 1, 2, 2, 1, 1, 2, 2]), np.array([1, 2, 3, 4, 5, 6, 7, 8])]
df = pd.DataFrame(np.random.randn(8), index=arrays, columns=['Col1'])
df.rename_axis(['A','B','C'], inplace=True)
print(df)
idx_lst = [(1,1), (2,2)]
test = df.loc(axis=0)[idx_lst]
print(test)