简体   繁体   中英

How to subset a dataframe that with a condition on two columns

I have a dataframe and I'm trying to get a subset of it where one column is contained in a list and the other column contains a word.

Context: 在此处输入图像描述

The table above is a sample of the dataframe I am working with, I am trying to subset the dataframe based on names that are contained in a list. I also want to subset it further by getting texts that contained 'named'

A sample of the code I use is:

names = ['a', 'an', 'my', 'by', 'mad', 'very', 'just', 'quite', 'one', 'actually', 'life', 'light', 'officially','his', 'old', 'this', 'all','the']
archive[archive['name'].isin(names)]['text'].str.contains('named')

But the code above returns a boolean series. I am trying to get a dataframe.

Chain both conditions with & for bitwise AND :

archive[archive['name'].isin(names) & archive['text'].str.contains('named')]

Simply do

archive[archive['name'].isin(names) & archive['text'].str.contains('named')]

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