Reputation: 16508
'g' in df.index
is the way to check whether 'g' is in my index. What if I want to do that for many elements? Say, I have an iterable object myIterable
(np.ndarray, list), and I want to get the elements that are in or not in my index...
inIterable = [x for x in myIterable if x in df.index]
notInIterable = [x for x in myIterable if x not in df.index]
will work, but is quite inefficient. What is the most efficient way of creating in
and notIn
?
Upvotes: 2
Views: 1161
Reputation: 394189
OK, If I understand correctly to test for membership you 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 the reverse one method would be to construct a pandas index and use difference
:
In [137]:
pd.Index(l).difference(df.index)
Out[137]:
Int64Index([7], dtype='int64')
Upvotes: 3
Reputation: 120
Use filter:
a = [1,2,3,4,5] # Index
b = [99,2,99,5] # MyIterable
in = filter(lambda x: x in a, b)
Note: your example should be:
in = [x for x in myIterable if x in df.index]
And "in" is a reserved word.
Upvotes: 1