1

So I have about 3,000 csv files and they are all named differently. Example, CDEE.csv and the structure is just one line containing the name and an amount.

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

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!

4
  • 3
    Are you actually doing anything with the DataFrame or just purely wanting to concat the files? As cat *.csv > csv_files.all is looking pretty promising right now... Commented Aug 18, 2017 at 14:38
  • I didn't see that until I edited the post. I'm with you. Commented Aug 18, 2017 at 15:15
  • @JonClements you should write that as an answer... Commented Aug 18, 2017 at 15:44
  • whoa, I'm trying now! Commented Aug 19, 2017 at 3:51

1 Answer 1

3

You need to use pd.concat which is documented here

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
  • 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="|" to use | as separator instead of the default ,

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.