【问题标题】:How to append/update new values to the rows of a existing csv file from a new csv file as a new column in python using pandas or something else如何使用 pandas 或其他东西将新值附加/更新到新 csv 文件中的现有 csv 文件的行作为 python 中的新列
【发布时间】:2020-11-25 12:44:28
【问题描述】:

旧文件

姓名,2015

杰克,205

吉尔,215

快乐,369

新建文件

姓名,2016

希尔,289

吉尔,501

劳夫,631

杰克,520

凯,236

喜悦,615

这是我想要的:

姓名、2015、2016

杰克, 205, 520

吉尔, 215, 501

欢乐,369、615

山, , 289

劳夫, , 631

凯, , 236

【问题讨论】:

    标签: python pandas dataframe csv merge


    【解决方案1】:

    感谢大家的回复

    我找到了如下方法:

    import pandas as pd
    
    old_file = pd.read_csv('old file.csv')
    new_file = pd.read_csv('new file.csv')
    
    old_file = old_file[old_file['Name'].isna() == False]
    new_file = new_file[new_file['Name'].isna() == False]
    
    data_combined = pd.merge(old_file, new_file, left_on='Name', right_on='Name', how='outer')
    print(data_combined.fillna(0).convert_dtypes())
    

    这给出了所需的输出:

       Name  2015  2016
    0  Jack   205   520
    1  Jill   215   501
    2   Joy   369   615
    3  Hill     0   289
    4  Rauf     0   631
    5   Kay     0   236
    

    【讨论】:

      【解决方案2】:

      我花了一段时间才把这个整理好。

      首先你提取文件的值:

          import csv
          
          with open('old.csv', 'r') as old_file:
              old_csv = [row for row in csv.reader(old_file)]
          with open('new.csv', 'r') as new_file:
              new_csv = [row for row in csv.reader(new_file)]
      

      那么我们需要获取新文件的名称:

          new_names = [row[0] for row in new_csv]
      

      然后我们可以遍历所有旧行,以便我们可以修改新文件并更新值

          for name, number in old_csv:
              index = None
      
              #Check if the name is already in the file
              if name in new_names:
                  index = new_names.index(name)
                  new_csv[index].append(number)
              
              #If not, add the new name with the number. This is maybe not neccessay
              else:
                  new_entry = [name, number]
                  new_csv.append(new_entry)   
      

      合并列表后,我们编写新文件

          with open('merged_file.csv', 'w') as merge_file:
              merger = csv.writer(merge_file)
              for row in new_csv:
                  merger.writerow(row)
      

      文件如下所示:

          Name, 2016, 2015
          Hill, 289
          Jill, 501, 215
          Rauf, 631
          Jack, 520, 205
          Kay, 236
          Joy, 615, 369
      

      不确定“名称”是否是标题。这需要在csv阅读器中添加

      【讨论】:

        【解决方案3】:

        这是一篇关于如何根据现有列在 Pandas DataFrame 中创建新列的帖子: https://www.geeksforgeeks.org/create-a-new-column-in-pandas-dataframe-based-on-the-existing-columns/

        如果您要在以下架构中解释您的错误会有所帮助:

        1. 发布您的代码
        2. 发布您的错误或返回代码
        3. 发布您所期望的内容

        【讨论】:

          猜你喜欢
          • 2019-01-09
          • 1970-01-01
          • 2018-05-24
          • 2015-08-02
          • 2017-06-30
          • 2019-03-26
          • 2020-09-10
          • 2020-08-28
          • 1970-01-01
          相关资源
          最近更新 更多