简体   繁体   English

Python连接多个没有头的CSV文件

[英]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: 请注意,几乎没有错误可以解决:

  • change glob.glob(".csv*") to glob.glob("*.csv") to get all files that end with .csv glob.glob(".csv*")更改为glob.glob("*.csv")以获取所有以.csv结尾的文件
  • to get exactly the following output: 获得确切的以下输出:
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.

相关问题 Python3 连接多个文件并忽略头部和尾部记录 - Python3 Concatenate multiple files and ignore header & trailer records 无法将多个 csv 文件导入到 Pandas 中并在 Python 中连接为一个 DataFrame - Failed to import multiple csv files into pandas and concatenate into one DataFrame in Python 将多个 csv 文件连接成具有相同标头的单个 csv - Python - Concatenating multiple csv files into a single csv with the same header - Python 如何根据列名将多个 csv 文件连接成一个文件,而无需在代码中键入每个列标题 - How to concatenate multiple csv files into one based on column names without having to type every column header in code 如何使用 python 使用列作为索引将多个 csv 文件连接到单个 csv 文件中 - How to concatenate multiple csv files into a single csv file using a column as index using python 将不同文件夹中的多个 csv 文件连接到 python 中的一个 csv 文件中 - Concatenate multiple csv files from different folders into one csv file in python Python-用逻辑连接多个文件 - Python - concatenate multiple files with logic 如何在 python 中连接两个 csv 文件 - How to concatenate two csv files in python 通过文件名的升序在python中连接csv文件 - Concatenate csv files in python by ascending order of filenames Python - 在特定目录中连接CSV文件 - Python - Concatenate CSV files in a specific directory
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM