Reputation: 1859
I have a pandas Dataframe which has a multiindex created using the columns userid
and itemid
. df looks like this
0 1 2
userid itemid
007 5000 9 4 3
007 4000 6 7 1
009 3000 1 2 3
I want to check if the index [007, 6000] exists in the dataframe df. How can I do that. If I run the following code there is an error TypeError: unhashable type: 'list'
.
if [007, 6000] in df.index:
print('it works')
Upvotes: 14
Views: 25292
Reputation: 10606
pd.MultiIndex
to list
and check for presence in list
import pandas as pd
mi = pd.MultiIndex.from_tuples(
[(7, 5000), (7, 4000), (8, 3000)], names=['usedId', 'itemId'])
df = pd.DataFrame([[9, 4, 3], [6, 7, 1], [1, 2, 3]], index=mi)
print('df:', df, sep='\n', end='\n\n')
print('mi:', mi, sep='\n', end='\n\n')
print('Check for elements in Multi-Index:')
print('\t(7, 4000) in mi.to_list():', (7, 4000) in mi.to_list())
print('\t(7, 99) in mi.to_list():', (7, 99) in mi.to_list())
df:
0 1 2
usedId itemId
7 5000 9 4 3
4000 6 7 1
8 3000 1 2 3
mi:
MultiIndex([(7, 5000),
(7, 4000),
(8, 3000)],
names=['usedId', 'itemId'])
Check for elements in Multi-Index:
(7, 4000) in mi.to_list(): True
(7, 99) in mi.to_list(): False
Upvotes: 0
Reputation: 863256
Use Index.isin
:
df = df.index.isin([('007','5000')])
print (df)
[ True False False]
Upvotes: 5
Reputation: 402852
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
Upvotes: 20