【问题标题】:How to merge two csv file vertically and keep the data formate (number to number, string to string)如何垂直合并两个csv文件并保持数据格式(数字到数字,字符串到字符串)
【发布时间】:2018-09-25 01:32:15
【问题描述】:

我想垂直合并两个 csv 文件。一个文件仅包含字符串(第一列,前三行)。第二个文件包含字符串和数字。

我可以打印出来。但是有问题将它们逐行保存到 csv 文件中。保持数据类型也有问题。 (数字到数字,字符串到字符串)。

以下是我使用的代码:

方法一:

import csv


file1 = ("/Users/yingdu/GitHub/20180807/String_.csv")
file2 = ("/Users/yingdu/GitHub/20180807/CovertFile_SampleData4.csv")


combined_file = ("/Users/yingdu/GitHub/20180807/combined_file.csv")
spreadsheet_filenames = [file1,file2]

for filename in spreadsheet_filenames:
    with open(filename, 'r') as csvfile:
        output = csv.reader(csvfile)
        for row in output:
            print row

以下是我的打印结果:

['SoftGenetics GeneMarker Trace Data Export']
['Raw Data']
['PAT_Ladder_1.fsa']
['Blue', 'Green', 'Yellow', 'Red', 'Orange']
['82.45', '97.65', '229.05', '85.25', '44.85']
['151.08', '167.48', '454.48', '136.68', '59.28']
['144.45', '161.25', '440.25', '133.65', '60.45']
['49.5', '65.9', '105.5', '69.1', '44.5']
['73.25', '109.45', '326.65', '70.85', '26.85']
['66.58', '97.18', '322.58', '65.38', '24.78']
['56.95', '77.35', '138.35', '91.95', '61.75']
['66.45', '79.65', '351.05', '69.25', '35.25']

以下是我用来编写将所有数据保存到新 csv 文件的 csv 文件的代码。我发现 csv 文件没有正确创建。

import csv


file1 = ("/Users/yingdu/GitHub/20180807/String_.csv")
file2 = ("/Users/yingdu/GitHub/20180807/CovertFile_SampleData4.csv")


combined_file = ("/Users/yingdu/GitHub/20180807/combined_file.csv")
spreadsheet_filenames = [file1,file2]

for filename in spreadsheet_filenames:
    with open(filename, 'r') as csvfile:
        output = csv.reader(csvfile)
        with open(Combined_File, mode='w') as Combined_File:
            for row in output:
                print row
                csv_writer = csv.writer(Combined_File, delimiter=',', quotechar='"', quoting=csv.QUOTE_MINIMAL)
                csv_writer.writerow(row)

方法二:

通过使用“dataframe.concat”。我将文件一和文件二视为两个对象(数据框)。合并是我期望的数据框。但是文件“combined_file.csv”不是由 df.to_csv 方法在这里创建/生成的。并且没有错误信息。

f1 = pd.read_csv(file1, header=None)
f2 = pd.read_csv(file2, header=None)
merged = pd.concat([f1, f2])
merged.to_csv(combined_file, index=None, header=None)

【问题讨论】:

    标签: python dataframe merge export-to-csv


    【解决方案1】:

    concat 将数据帧列表作为其第一个参数。

    试试:

    merged = pd.concat([f1, f2])
    

    【讨论】:

    • 我更新了代码。它已正确合并到“合并”的新数据框。但我无法使用“merged.to_csv”将数据框保存到 csv 文件,这是异常的。它没有给我任何错误信息。
    • 这看起来很奇怪。您确定您检查了您尝试保存 csv 文件的位置吗?也许尝试改变路径?
    • 因为它能够读取file1和file 2,所以路径“/Users/yingdu/GitHub/20180807”退出。我以为 /combined_file.csv 应该是这样创建的。
    • 文件夹可能被写保护了。这就是为什么我建议您更改路径并仔细检查您的脚本是否工作正常。然后你就知道要解决什么了。
    • 哈哈太好了!如果有帮助,请接受答案。 :)
    【解决方案2】:

    您正在创建文件两次! open(Combined_File, mode='w') 覆盖文件,因为它在循环内,你只会从最后一个文件中获取数据。

    另一个提示是,您可以使用 writerows() 通过一次调用写入多行,并且它需要一个可迭代对象,因此您只需传递 csv_input 即可写入所有内容:

    import csv
    
    file1 = "/Users/yingdu/GitHub/20180807/String_.csv"
    file2 = "/Users/yingdu/GitHub/20180807/CovertFile_SampleData4.csv"
    spreadsheet_filenames = [file1, file2]
    combined_file = "/Users/yingdu/GitHub/20180807/combined_file.csv"
    
    with open(combined_file, 'w') as output_file: # create output outside the for loop
         csv_output = csv.writer(output_file, delimiter=',') 
         for filename in spreadsheet_filenames:
             with open(filename) as input_file:
                 csv_input = csv.reader(input_file, delimiter=',')
                 csv_output.writerows(csv_input)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-09
      • 2017-11-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多