【发布时间】:2020-07-05 19:23:39
【问题描述】:
我有 2 个 csv 文件,news.csv(主 csv 文件)和 dictionary1.csv 我能够找出 news.csv 中的术语是否存在于任何字典 csvs 中,返回 1 表示存在,返回 0不存在,以下是我的代码:
news=pd.read_csv("news.csv")
dictionary=pd.read_csv("dictionary1.csv")
pattern='|'.join(dictionary['Lists'])
news["exist/not exist"] = np.where(
news["STORY"].str.contains(pattern, na=False, case=False),
1, 0
)
但是,我不知道如何从 2 个 csv 中返回匹配的列表。以下是我的 news.csv 示例
ID STORY
1 The weather today is Sunny, but tomorrow it is expected to be rainy
2 In UK, there are 4 seasons, winter,summer, autumn and spring
3 Food is essential in life
我的 dictionary1.csv 示例
Lists
Sunny
Rainy
Winter
Summer
Spring
Autumn
我想要的输出是
ID STORY exist/not exist similar
1 The weather today is Sunny, but tomorrow it is expected to be rainy 1 sunny, rainy
2 In UK, there are 4 seasons, winter,summer, autumn and spring 1 winter,summer,autumn,spring
3 Food is essential in life 0 -
【问题讨论】: