Два списка и выписка - PullRequest
       1

Два списка и выписка

0 голосов
/ 28 сентября 2018

Вот мой код, я хочу получить результат, содержащий b и c, но он выдаёт мне ошибку.

df = pd.read_excel('C:Test 0926.xlsx')

df.drop_duplicates(['mention'],inplace=True)

a = df['mention'].str.lower()
searchfor =[some words in it]

b = a[a.str.contains('|'.join(searchfor))]
opposite = [some words in it]

c = a[a.str.contains('|' .join(opposite))]
def check_it(sentences):
    if b and c in sentences:
        return sentences

d = a.apply(lambda x:check_it(x))
print(d)

Кто-нибудь может помочь с этим?

1 Ответ

0 голосов
/ 28 сентября 2018

Я думаю, вам нужно объединить логические маски и фильтры по boolean indexing:

df = pd.DataFrame({
        'mention':['this is nice', 'nice', 'this is nice and bad', 'this is bad']
})

print (df)
                mention
0          this is nice
1                  nice
2  this is nice and bad
3           this is bad

df.drop_duplicates(['mention'],inplace=True)

a = df['mention'].str.lower()
opposite = ['nice']
searchfor = ['bad']


d = a[a.str.contains('|'.join(searchfor)) & a.str.contains('|' .join(opposite))]
print(d)
2    this is nice and bad
Name: mention, dtype: object
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...