在python中使用pandas将csv文件附加到一个

Jac*_*uer 5 python csv pandas

我在一个目录中有n个文件需要合并为一个.它们具有相同数量的列,例如,内容test1.csv是:

test1,test1,test1  
test1,test1,test1  
test1,test1,test1  
Run Code Online (Sandbox Code Playgroud)

同样,内容test2.csv是:

test2,test2,test2  
test2,test2,test2  
test2,test2,test2  
Run Code Online (Sandbox Code Playgroud)

我希望final.csv看起来像这样:

test1,test1,test1  
test1,test1,test1  
test1,test1,test1  
test2,test2,test2  
test2,test2,test2  
test2,test2,test2  
Run Code Online (Sandbox Code Playgroud)

但相反它出来是这样的:

test file 1,test file 1.1,test file 1.2,test file 2,test file 2.1,test file 2.2  
,,,test file 2,test file 2,test file 2  
,,,test file 2,test file 2,test file 2  
test file 1,test file 1,test file 1,,,  
test file 1,test file 1,test file 1,,,  
Run Code Online (Sandbox Code Playgroud)

有人可以帮我弄清楚这里发生了什么吗?我在下面粘贴了我的代码:

import csv
import glob
import pandas as pd
import numpy as np 

all_data = pd.DataFrame() #initializes DF which will hold aggregated csv files

for f in glob.glob("*.csv"): #for all csv files in pwd
    df = pd.read_csv(f) #create dataframe for reading current csv
    all_data = all_data.append(df) #appends current csv to final DF

all_data.to_csv("final.csv", index=None)
Run Code Online (Sandbox Code Playgroud)

jez*_*ael 5

我认为还有更多问题:

  1. 我删除import csvimport numpy as np,因为在这个演示,他们没有使用(但也许他们缺少的,线,因此他们可以导入)
  2. 我创建了所有数据帧的列表dfs,其中附加了数据帧dfs.append(df).然后我使用函数concat将此列表加入到最终数据帧.
  3. 在函数中read_csv我添加了参数header=None,因为主要问题是read_csv将第一行读为header.
  4. 在函数中to_csv我添加了header=None用于省略标题的参数.
  5. 我将文件夹添加test到最终目标文件,因为如果使用函数,glob.glob("*.csv")您应该将输出文件作为输入文件读取.

解:

import glob
import pandas as pd

all_data = pd.DataFrame() #initializes DF which will hold aggregated csv files

#list of all df
dfs = []
for f in glob.glob("*.csv"): #for all csv files in pwd
    #add parameters to read_csv
    df = pd.read_csv(f, header=None) #create dataframe for reading current csv
    #print df
    dfs.append(df) #appends current csv to final DF
all_data = pd.concat(dfs, ignore_index=True)
print all_data
#       0      1      2
#0  test1  test1  test1
#1  test1  test1  test1
#2  test1  test1  test1
#3  test2  test2  test2
#4  test2  test2  test2
#5  test2  test2  test2
all_data.to_csv("test/final.csv", index=None, header=None)
Run Code Online (Sandbox Code Playgroud)

下一个解决方案类似.
我想补充的参数header=None,以read_csvto_csv,并添加参数ignore_index=Trueappend.

import glob
import pandas as pd

all_data = pd.DataFrame() #initializes DF which will hold aggregated csv files

for f in glob.glob("*.csv"): #for all csv files in pwd
    df = pd.read_csv(f, header=None) #create dataframe for reading current csv
    all_data = all_data.append(df, ignore_index=True) #appends current csv to final DF
print all_data
#       0      1      2
#0  test1  test1  test1
#1  test1  test1  test1
#2  test1  test1  test1
#3  test2  test2  test2
#4  test2  test2  test2
#5  test2  test2  test2

all_data.to_csv("test/final.csv", index=None, header=None)
Run Code Online (Sandbox Code Playgroud)