![](/img/trans.png)
[英]python pandas new row attached to last one in csv when using to_csv in append mode
[英]using pandas in python to append csv files into one
我在一個目錄中有n個文件需要合並為一個。 它們具有相同數量的列,例如, test1.csv
的內容是:
test1,test1,test1
test1,test1,test1
test1,test1,test1
同樣, test2.csv
的內容是:
test2,test2,test2
test2,test2,test2
test2,test2,test2
我希望final.csv看起來像這樣:
test1,test1,test1
test1,test1,test1
test1,test1,test1
test2,test2,test2
test2,test2,test2
test2,test2,test2
但相反它出來是這樣的:
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,,,
有人可以幫我弄清楚這里發生了什么嗎? 我在下面粘貼了我的代碼:
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)
我認為還有更多問題:
import csv
並import numpy as np
,因為在這個演示中它們沒有被使用(但也許它們缺少,行可以導入它們) dfs
列表,其中數據幀由dfs.append(df)
追加。 然后我使用函數concat
將此列表加入到最終數據幀。 read_csv
我添加了參數header=None
,因為主要問題是read_csv
將第一行讀作header
。 to_csv
我添加了參數header=None
來省略標題。 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)
下一個解決方案類似。
我將參數header=None
添加到read_csv
和to_csv
並添加參數ignore_index=True
以append
。
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)
你可以concat
。 讓df1
成為您的第一個數據幀, df2
成為第二個數據幀,您可以:
df = pd.concat([df1,df2],ignore_index=True)
ignore_index
是可選的,如果您不介意單個數據幀的原始索引,可以將其設置為True
。
當你想要的只是創建一個csv文件時, pandas
不是一個可以使用的工具,你可以簡單地將每個csv寫入一個新文件:
import glob
with open("out.csv","w") as out:
for fle in glob.glob("*.csv"):
with open(fle) as f:
out.writelines(f)
或者如果您願意,可以使用csv lib:
import glob
import csv
with open("out.csv", "w") as out:
wr = csv.writer(out)
for fle in glob.glob("*.csv"):
with open(fle) as f:
wr.writerows(csv.reader(f))
創建一個大型數據幀只是為了最終寫入磁盤沒有任何意義,而且如果你有很多大文件,它甚至可能是不可能的。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.