[英]Python Concatenate Multiple CSV files with no header
So I have about 3,000 csv files and they are all named differently. 因此,我大约有3,000个csv文件,而且它们的名称都不同。 Example, CDEE.csv and the structure is just one line containing the name and an amount.
例如,CDEE.csv,结构只有一行,其中包含名称和金额。
CDEE | 3993
I tried to concatenate and I keep getting 我试图串联,并且不断
CDEE | 3993 | AASE| 3939 .........
but I want 但我想要
CDEE | 3992
AASE | 3939
xxxx | yyyy
Here is the code: import pandas as pd import glob, os 这是代码:将pandas导入为pd import glob,os
path = "/home/username/myfolder"
os.chdir(path)
results = pd.DataFrame([])
for counter, file in enumerate(glob.glob(".csv*")):
namedf = pd.read_csv(file,skiprows=0, usecols=[1,2,3])
results = results.append(namedf)
results.to_csv('Combined.csv')
Thanks for any help, I really appreciate it! 感谢您的帮助,我非常感谢!
You need to use pd.concat
which is documented here 您需要使用此处记录的
pd.concat
import pandas as pd
import os
import glob
path = "."
os.chdir(path)
results = pd.DataFrame()
for counter, current_file in enumerate(glob.glob("*.csv")):
namedf = pd.read_csv(current_file, header=None, sep="|")
print(namedf)
results = pd.concat([results, namedf])
results.to_csv('Combined.csv', index=None, header=None, sep="|")
Note that there are few mistakes to fix: 请注意,几乎没有错误可以解决:
glob.glob(".csv*")
to glob.glob("*.csv")
to get all files that end with .csv
glob.glob(".csv*")
更改为glob.glob("*.csv")
以获取所有以.csv
结尾的文件 CDEE|3992 AASE|3939 xxxx|yyyy
You need to call df.to_csv
with index=None
to not write the index, header=None
to not write the header and sep="|"
您需要使用
index=None
调用df.to_csv
来不写索引, header=None
来不写头和sep="|"
to use |
使用
|
as separator instead of the default ,
作为分隔符而不是默认值
,
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.