I have a dataframe df
:
data = {'id':[12,112],
'idlist':[[1,5,7,12,112],[5,7,12,111,113]]
}
df=pd.DataFrame.from_dict(data)
which looks like this:
id idlist
0 12 [1, 5, 7, 12, 112]
1 112 [5, 7, 12, 111, 113]
I need to check and see if id
is in the idlist
, and select or flag it. I have tried variations of the following and receive the commented error:
df=df.loc[df.id.isin(df.idlist),:] #TypeError: unhashable type: 'list'
df['flag']=df.where(df.idlist.isin(df.idlist),1,0) #TypeError: unhashable type: 'list'
Some possible other methods to a solution would be .apply
in a list comprehension?
I am looking for a solution here that either selects the rows where id
is in idlist
, or flags the row with a 1 where id
is in idlist
. The resulting df
should be either:
id idlist
0 12 [1, 5, 7, 12, 112]
or:
flag id idlist
0 1 12 [1, 5, 7, 12, 112]
1 0 112 [5, 7, 12, 111, 113]
Thanks for the help!