首页 > 解决方案 > 如何使用 pandas 在 python 中读取两个 CSV 文件并将它们相互附加?

问题描述

我正在开发一个需要我导入 CSV 文件并将每一行显示为横幅的应用程序。我正在使用的当前方法成功显示横幅,但它每次都在覆盖我正在使用的数组。我想做的是读取一个 CSV 文件,将其附加到一个数组中,然后可以选择将另一个 CSV 文件读取到同一个数组中。这样我就可以运行另一种方法,该方法允许我读取该数组并将两组数据保存回另一个 CSV 文件。

这是我检索数据的方法

def GetData(root, self, data_dictionary, data_arr):
        temp = Tk()
        temp.wm_state('iconic')
    
    filetypes = (
        ('CSV', '*.csv'),
        ('All files', '*.*')
    )

    filename = fd.askopenfilename(
        title='Open a file',
        initialdir='./geodata/',
        filetypes=filetypes)
    
    if filename:
        with open(filename):                
            data_arr = pd.read_csv(filename)
            temp.destroy()
            return data_arr
    else:
        Alert(title='Error', text='Please select a file')
        temp.destroy()
        return

然后我有另一个调用该方法的方法:

def upload_file(root, self):
    global data_arr
    values = GetData(self, root,  data_arr) #Where data_arr is defined in the global scope as []
    data_arr.append(values)
    print(data_arr)

我第一次上传文件时,它会打印以下内容:

[(  publicid   eventtype                origintime          modificationtime   longitude  latitude  magnitude  ...  usedphasecount usedstationcount  magnitudestationcount minimumdistance azimuthalgap originerror magnitudeuncertainty
0  testid1  earthquake  2021-08-24T04:49:39.852Z  2021-08-24T04:54:00.721Z  177.428024  -37.3592   2.342669  ...              16               12                      6        0.486189   258.299072    0.542223                    0     
1  testid2  earthquake  2021-08-24T04:49:39.852Z  2021-08-24T04:54:00.721Z  177.428024  -37.3592   2.342669  ...              16               12                      6        0.486189   258.299072    0.542223                    0     

这种方法的问题在于,当我尝试上传第二个文件时,它会打印出这样的结果:

[(  publicid   eventtype                origintime          modificationtime   longitude  latitude  magnitude  ...  usedphasecount usedstationcount  magnitudestationcount minimumdistance azimuthalgap originerror magnitudeuncertainty
0  testid1  earthquake  2021-08-24T04:49:39.852Z  2021-08-24T04:54:00.721Z  177.428024  -37.3592   2.342669  ...              16               12                      6        0.486189   258.299072    0.542223                    0     
1  testid2  earthquake  2021-08-24T04:49:39.852Z  2021-08-24T04:54:00.721Z  177.428024  -37.3592   2.342669  ...              16               12                      6        0.486189   258.299072    0.542223                    0     

[2 rows x 21 columns],), (  publicid   eventtype                origintime          modificationtime   longitude  latitude  magnitude  ...  usedphasecount usedstationcount  magnitudestationcount minimumdistance azimuthalgap originerror magnitudeuncertainty
0  testid1  earthquake  2021-08-24T04:49:39.852Z  2021-08-24T04:54:00.721Z  177.428024  -37.3592   2.342669  ...              16               12                      6        0.486189   258.299072    0.542223                    0     
1  testid2  earthquake  2021-08-24T04:49:39.852Z  2021-08-24T04:54:00.721Z  177.428024  -37.3592   2.342669  ...              16               12                      6        0.486189   258.299072    0.542223                    0     

[2 rows x 21 columns],)]

有没有办法可以读取第二个 CSV 文件并将其与其他数据一起存储在原始数组中,如下所示:

[(  publicid   eventtype                origintime          modificationtime   longitude  latitude  magnitude  ...  usedphasecount usedstationcount  magnitudestationcount minimumdistance azimuthalgap originerror magnitudeuncertainty
0  testid1  earthquake  2021-08-24T04:49:39.852Z  2021-08-24T04:54:00.721Z  177.428024  -37.3592   2.342669  ...              16               12                      6        0.486189   258.299072    0.542223                    0     
1  testid2  earthquake  2021-08-24T04:49:39.852Z  2021-08-24T04:54:00.721Z  177.428024  -37.3592   2.342669  ...              16               12                      6        0.486189   258.299072    0.542223                    0     
3  testid3  earthquake  2021-08-24T04:49:39.852Z  2021-08-24T04:54:00.721Z  177.428024  -37.3592   2.342669  ...              16               12                      6        0.486189   258.299072    0.542223                    0     
4  testid4  earthquake  2021-08-24T04:49:39.852Z  2021-08-24T04:54:00.721Z  177.428024  -37.3592   2.342669  ...              16               12                      6        0.486189   258.299072    0.542223                    0     

[4 rows x 21 columns],)]

标签: pythonpandascsv

解决方案


问题在于,每次调用 时upload_file,实际上都是在将一个数组(由 返回GetData)附加到一个数组中。因此,您最终会在原始数组中得到一个新元素,并且该元素的类型是数组本身。尝试使用data_arr.extend(values)而不是data_arr.append(values).

例子:

foo = ["a", "b"]
bar = ["c", "d"]
print(foo.append(bar))  # ['a', 'b', ['c', 'd']]

baz = ["e", "f"]
qux = ["g", "h"]
print(baz.extend(qux))  # ['e', 'f', 'g', 'h']

推荐阅读