如何不完全匹配2个csv文件

问题描述 投票:0回答:1

我有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
1个回答
0
投票

您是否考虑过Fuzzywuzzy python库?这是SeatGeek开源的字符串匹配库。它根据不完全匹配提供匹配分数,然后您确定哪个阈值接近匹配即可。

[根据我的经验,我使用它来匹配来自不同数据源的医师姓名(例如,有些人说“ Dr.”,有些人说“ MD”,某些名字紧缩,有些姓氏由于姓氏而改变)。] >

这里有2个到库的链接。

https://chairnerd.seatgeek.com/fuzzywuzzy-fuzzy-string-matching-in-python/

https://github.com/seatgeek/fuzzywuzzy

© www.soinside.com 2019 - 2024. All rights reserved.