00__00__00
00__00__00

Reputation: 5347

Check if a index value exists in a list

I have a pandas dataframe. My dataframe as an index

import pandas as pd
df=pd.DataFrame(data=['a','b','c']).T
df.columns='myvar'
df.set_index(['myvar'])

Then I have some lists containing some or none of the index values

list_of_interest_1=['b','c','d','z']
list_of_interest_2=['a']
list_of_interest_3=['duck']

I would like to add to my dataframe a few boolean columns which mark whether the corresponding index value was in one of the lists In the example:

df=
   wasinlist1  wasinlist2  wasinlist3
a  False      True         False
b  True       False        False
c  True       False        False

Upvotes: 0

Views: 1556

Answers (2)

jpp
jpp

Reputation: 164773

This seems to be what you want:

import pandas as pd

df = pd.DataFrame([['a'], ['b'], ['c']], columns=['myvar'])
df = df.set_index('myvar')

list_of_interest_1 = ['b','c','d','z']
list_of_interest_2 = ['a']
list_of_interest_3 = ['duck']

for i, j in enumerate([list_of_interest_1, list_of_interest_2, list_of_interest_3], 1):
    df['InList'+str(i)] = df.index.isin(j)

#       InList1 InList2 InList3
# myvar                        
# a       False    True   False
# b        True   False   False
# c        True   False   False

Upvotes: 1

jezrael
jezrael

Reputation: 863256

Use isin with dict comprehension:

df=pd.DataFrame(['a','b','c'])
df.columns=['myvar']
df = df.set_index(['myvar'])

list_of_interest_1=['b','c','d','z']
list_of_interest_2=['a']
list_of_interest_3=['duck']

d = {'list_of_interest_1':list_of_interest_1, 
    'list_of_interest_2':list_of_interest_2, 
    'list_of_interest_3': list_of_interest_3}

for k, v in d.items():
   df[k] = df.index.isin(v)

df = df.sort_index(axis=1)
print(df)

       list_of_interest_1  list_of_interest_2  list_of_interest_3
myvar                                                            
a                   False                True               False
b                    True               False               False
c                    True               False               False

Upvotes: 3

Related Questions