Filtering a pandas column which contains a list in other column?
I have a pandas dataframe pd like this https://imgur.com/a/6TM3B3o
I want to filter the df_participants column which contains pd['df_pair'][0]
, and the expected result should be a new pd1 containing only rows that pd['df_pair'][0]
is a subset of df_paticipants like this https://imgur.com/EzCcuh3
I have no idea how to do that. I have tried with .isin()
or pd1 = pd[pd['df_participants'].str.contains(pd['df_pair'][0])]
but it does not work. Is there any idea?
Solution 1:
I think pd
variable is not good idea for DataFrame
, better is use df
:
#remove nans rows
df = df.dropna(subset=['df_participants'])
#get rows if subset
df = df[df.df_participants.map(set(df['df_pair'][0]).issubset)]