【发布时间】:2020-02-09 05:01:39
【问题描述】:
我有 2 个 csv 文件,dictionary.csv 和 file.csv,我想检查 dictionary.csv 中的单词是否存在于 file.csv 中。 dictionary.csv 中的某些行包含超过 2 个单词,我想知道是否有办法做到这一点,
如果行中有 3 个单词,并且 file.csv 中匹配的行中至少有 2/3 个单词,则返回 1,否则返回 0
如果行中有 2 个单词,并且 file.csv 中匹配的行中至少有 1/2 个单词,则返回 1,否则返回 0
以下是我目前的代码,它正在精确匹配
file=pd.read_csv("file.csv")
dictionary=pd.read_csv("dictionary.csv")
pattern='|'.join(dictionary)
news["contain diseases1"] = np.where(
news["STORY"].str.contains(pattern, na=False),
1, 0
)
news.to_csv("clues.csv")
为了进一步帮助您理解我的问题,以下是dictionary.csv 和file.csv 的内容
dictionary.csv
sigmoid colon cancer
site specific early onset breast cancer syndrome
skin cancer
file.csv
id STORY
0 Ari have a colon cancer
1 Cancer is an epidemic
2 Breast cancer can happen to both genders
我应该从这些文件中得到的输出是
clue.csv
id STORY contain diseases1
0 Ari have a colon cancer 1
1 Cancer is an epidemic 1
2 Breast cancer can happen to both genders 1
3 Prioritizing the health of skin 0
4 A specific camping site is only for early birds 0
到目前为止,由于我现在拥有的代码是完全匹配的,所以我一直得到 0
【问题讨论】:
标签: python-3.x pandas csv dictionary