【问题标题】:Two lists and statement两个列表和声明
【发布时间】:2019-03-04 02:40:56
【问题描述】:

这是我的代码,我想得到包含 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)

有人可以帮忙吗?

【问题讨论】:

标签: python pandas list lambda


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-05-27
    • 2017-12-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-18
    相关资源
    最近更新 更多