pandas - Python Concatenate Multiple CSV files with no header -


so have 3,000 csv files , named differently. example, cdee.csv , structure 1 line containing name , amount.

cdee | 3993 

i tried concatenate , keep getting

cdee | 3993 | aase| 3939 ......... 

but want

cdee | 3992 aase | 3939 xxxx | yyyy 

here code: import pandas pd import glob, os

path = "/home/username/myfolder"  os.chdir(path) results = pd.dataframe([])  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 help, appreciate it!

you need use pd.concat documented here

import pandas pd import os import glob  path = "." os.chdir(path) results = pd.dataframe()  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 there few mistakes fix:

  • change glob.glob(".csv*") glob.glob("*.csv") files end .csv
  • to following output:
 cdee|3992 aase|3939 xxxx|yyyy 

you need call df.to_csv index=none not write index, header=none not write header , sep="|" use | separator instead of default ,


Comments

Popular posts from this blog

Is there a better way to structure post methods in Class Based Views -

performance - Why is XCHG reg, reg a 3 micro-op instruction on modern Intel architectures? -

jquery - Responsive Navbar with Sub Navbar -