Perhaps use multi_df.loc[look_up_list].dropna()
.
import pandas as pd
df = pd.DataFrame(
{'id': range(1, 9),
'code': ['one', 'one', 'two', 'three',
'two', 'three', 'one', 'two'],
'colour': ['black', 'white', 'white', 'white',
'black', 'black', 'white', 'white'],
'texture': ['soft', 'soft', 'hard', 'soft', 'hard',
'hard', 'hard', 'hard'],
'shape': ['round', 'triangular', 'triangular', 'triangular', 'square',
'triangular', 'round', 'triangular']
}, columns=['id', 'code', 'colour', 'texture', 'shape'])
multi_df = df.set_index(
['code', 'colour', 'texture', 'shape']).sort_index()['id']
# define the list of indices that I want to look up for in multi_df
look_up_list = [('two', 'white', 'hard', 'triangular'), (
'five', 'black', 'hard', 'square'), ('four', 'black', 'hard', 'round')]
subdf = multi_df.loc[look_up_list].dropna()
print(subdf ** 2)
yields
(two, white, hard, triangular) 9
(two, white, hard, triangular) 64
Name: id, dtype: float64
Note:
multi_df
as defined above is a Series, not a DataFrame. I don't think that affects the solution though.- The code you posted above raises
IndexingError: Too many indexers
so I'm guessing (a little bit) at the intention of the code.