【问题标题】:I want to check if a value in csv exist in another csv file return 1我想检查另一个 csv 文件中是否存在 csv 中的值返回 1
【发布时间】:2019-10-07 14:18:18
【问题描述】:

我有 2 个 csv 文件:一个是 dictionary.csv,另一个是 file.csv,其中包含很多单词。我想检查dictionary.csv 中的单词是否存在于file.csv 的特定列中。

如果存在,则应创建一个新文件new.csv。该文件应包含 file.csv 中的所有数据,但有一个额外的列,如果存在则写入 1,如果不存在则写入 0。

这是我的脚本:

import csv
import pandas as pd

news=pd.read_csv("file.csv")

dictionary=pd.read_csv("dictionary.csv", squeeze=True)

pattern = '|'.join(dictionary)

exist=news['sentences'].str.contains(pattern, na=False)

with open('new.csv', 'w') as outFile:
    for cols in exist:
        if pattern in exist:
            outFile.write(exist, "1")

结果,我得到一个空的 csv 文件,我想我可能遗漏了什么。

file.csv
id      sentences
0        Roses are red
1        burgers are delicious
dictionary.csv
red
blue
green

new.csv 文件应包含以下输出:

id      sentences                exist/not exist
0        Roses are red               1
1        burgers are delicious       0

【问题讨论】:

    标签: python-3.x pandas csv export-to-csv


    【解决方案1】:

    您可以使用numpy.where 创建新列,使用pandas.DataFrame.to_csv 将结果写入新文件。

    news["exist/not exist"] = np.where(
        news["sentences"].str.contains('|'.join(dictionary), na=False),
        1, 0
    )
    
    news.to_csv("name.csv", index=False)
    

    【讨论】:

      【解决方案2】:

      我们有

      file
      
         id              sentences
      0   0          Roses are red
      1   1  burgers are delicious
      

      dictionary
             0
      0    red
      1   blue
      2  green
      

      你可以这样做:

      words=list(dictionary[0])
      file['exist']=file['sentences'].apply(lambda x: len([i for i in words if i in x]))
      print(file)
      
         id              sentences  exist
      0   0          Roses are red      1
      1   1  burgers are delicious      0
      

      然后你就可以保存了:

      file.to_csv('new.csv', index=False)
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多