【问题标题】:How to return the list if they exist in both csv files如果它们存在于两个 csv 文件中,如何返回列表
【发布时间】: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               -

【问题讨论】:

    标签: python pandas numpy csv


    【解决方案1】:

    首先需要使用re.escape 转义字符串以正确解析特殊的正则表达式字符,然后添加/b/b 用于单词边界 - 空格之间的提取值。

    然后使用Series.str.findallre.I 忽略大小写,然后添加Series.str.join

    import re
    
    pattern = '|'.join(r"\b{}\b".format(re.escape(x)) for x in dictionary['Lists'])
    
    news['similar'] = news["STORY"].str.findall(pattern, flags=re.I).str.join(', ')
    print (news)
       ID                                              STORY  exist/not exist  \
    0   1  The weather today is Sunny, but tomorrow it is...                1   
    1   2  In  UK, there are 4 seasons, winter,summer, au...                1   
    2   3                          Food is essential in life                0   
    
                              similar  
    0                    Sunny, rainy  
    1  winter, summer, autumn, spring  
    2                                  
    

    或者通过Series.str.lower将两个值都转换为小写:

    news['similar'] = news["STORY"].str.lower().str.findall(pattern.lower()).str.join(', ')
    print (news)
       ID                                              STORY  exist/not exist  \
    0   1  The weather today is Sunny, but tomorrow it is...                1   
    1   2  In  UK, there are 4 seasons, winter,summer, au...                1   
    2   3                          Food is essential in life                0   
    
                              similar  
    0                    sunny, rainy  
    1  winter, summer, autumn, spring  
    2                                  
    

    【讨论】:

    • 我的类似专栏一无所获
    • @strawberrylatte - 两种解决方案?
    • @strawberrylatte - 对我来说工作得很好,也许是一些与数据相关的问题。数据是保密的吗?可以分享吗?
    • 数据其实不大,但不知道在哪里可以分享
    • @strawberrylatte - 为我工作pattern = '|'.join(r"\b{}\b".format(re.escape(x)) for x in dictionary['Lists'])
    猜你喜欢
    • 2022-08-11
    • 2022-01-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-26
    • 2021-09-23
    • 2021-12-15
    • 2022-06-16
    相关资源
    最近更新 更多