两份清单和声明

时间:2018-09-28 06:29:24

标签: python pandas list lambda

这是我的代码,我想获取包含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 :(得分:2)

我认为您需要同时链接布尔掩码和通过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