简体   繁体   中英

How to select rows from a python DataFrame by using isin?

I have a pandas dataframe. I want to use isin to choose rows that the value of column A is in column B .

For example: I have my_df , and I want to select row 0 and 2, because [aa, ab] and [bc, bd] both in A and B respectively.

my_df = pd.DataFrame({'A':[['aa','ab'],['aa','ac'],['bc','bd']],
                     'B':[[['aa','ab'],['ba','bb'],['bc','bd']],[['aa','ab'],['ba','bb']],[['aa','ab'],['bc','bd']]]})
my_df

    A           B
0   [aa, ab]    [[aa, ab], [ba, bb], [bc, bd]]
1   [aa, ac]    [[aa, ab], [ba, bb]]
2   [bc, bd]    [[aa, ab], [bc, bd]]

I want this:

    A           B
0   [aa, ab]    [[aa, ab], [ba, bb], [bc, bd]]
2   [bc, bd]    [[aa, ab], [bc, bd]]

I think you need apply with in :

print (my_df.apply(lambda x: x.A in x.B, axis=1))
0     True
1    False
2     True
dtype: bool
print (my_df[my_df.apply(lambda x: x.A in x.B, axis=1)])
          A                               B
0  [aa, ab]  [[aa, ab], [ba, bb], [bc, bd]]
2  [bc, bd]            [[aa, ab], [bc, bd]]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM