python - Check if set of elements in pandas index -


apparently,

'g' in df.index 

is the way check whether 'g' in index. if want many elements? say, have iterable object myiterable (np.ndarray, list), , want elements in or not in index...

initerable = [x x in myiterable if x in df.index] notiniterable = [x x in myiterable if x not in df.index] 

will work, quite inefficient. efficient way of creating in , notin?

ok, if understand correctly test membership can use intersection:

in [132]:  l=[1,2,7] df = pd.dataframe({'a':[0,1,2,3,4]}) df.index.intersection(l) out[132]: int64index([1, 2], dtype='int64') 

for reverse 1 method construct pandas index , use difference:

in [137]:  pd.index(l).difference(df.index) out[137]: int64index([7], dtype='int64') 

Comments