Python Forum

Full Version: Pandas merge csv files
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi,

I was trying to merge two csv files and it worked BUT the first column of the beginning of the merged file starts with a "," (see image). Any ideas?
Thanks

code

from os import chdir
from glob import glob
import pandas as pdlib

def merge_csv(list_raw_files, file_out):
   # Consolidate all CSV files into one object
   result_obj = pdlib.concat([pdlib.read_csv(file) for file in list_raw_files])
   # Convert the above object into a csv file and export
   result_obj.to_csv(file_out, index=False, encoding="utf-8")
    
# Move to the path that holds our CSV files
file_folder = 'my_path'
chdir(file_folder)

# List all CSV files in the working dir
list_raw_files = [f for f in listdir(file_folder) if isfile(join(file_folder, f))]
#print(list_raw_files)

file_out = "merged_file.csv"
merge_csv(list_raw_files, file_out)
Are the separators commas?

Whatever.
You could put them together as strings.

file1 = "/path/file1.csv"
file2 = "/path/file2.csv"
outfile = "/path/all_together.csv"
all_together = ""

with open(file1, 'r') as f:
    all_together = f.read()
    
with open(file2, 'r') as f:
    all_together = f"{all_together}{f.read()}"

with open(outfile, 'w') as f:
    f.write(all_together)
(Dec-16-2019, 10:01 AM)Axel_Erfurt Wrote: [ -> ]Are the separators commas?

Whatever.
You could put them together as strings.

file1 = "/path/file1.csv"
file2 = "/path/file2.csv"
outfile = "/path/all_together.csv"
all_together = ""

with open(file1, 'r') as f:
    all_together = f.read()
    
with open(file2, 'r') as f:
    all_together = f"{all_together}{f.read()}"

with open(outfile, 'w') as f:
    f.write(all_together)

yes they are commas! ok I would try your solution.