pandas dataframe check if index exists in a multi index
For this -
df
0 1 2
userid itemid
7 5000 9 4 3
4000 6 7 1
9 3000 1 2 3
df.index.values
array([(7, 5000), (7, 4000), (9, 3000)], dtype=object)
You can use df.index.isin
.
df.index.isin([(7, 5000)])
array([ True, False, False], dtype=bool)
This gives you a mask corresponding to where that value can be found. If you just want to know whether it exists or not, use np.ndarray.any
in conjunction with isin
.
df.index.isin([(7, 5000)]).any()
True
df.index.isin([(7, 6000)]).any()
False