【问题标题】:how to not exact matching 2 csv files如何不完全匹配 2 个 csv 文件
【发布时间】: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


    【解决方案1】:

    你考虑过fuzzywuzzy python 库吗?它是一个由 SeatGeek 开源的字符串匹配库。它根据不完美匹配提供匹配分数,然后您决定哪个阈值足够接近以成为匹配。

    根据我的经验,我用它来匹配来自不同数据源的医生姓名(例如,有些人说“博士”,有些人说“MD”,有些名字是缩写的,有些姓氏因未婚姓而改变)。

    这是图书馆的 2 个链接。

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

    https://github.com/seatgeek/fuzzywuzzy

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-04-18
      • 2019-09-22
      相关资源
      最近更新 更多