将 pd.MultiIndex
转换为 list
并检查其是否存在于 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
isin
。另外,它们是整数还是字符串? - cs95