Pandas merge csv files - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: Data Science (https://python-forum.io/forum-44.html) +--- Thread: Pandas merge csv files (/thread-23204.html) |
Pandas merge csv files - karlito - Dec-16-2019 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) RE: Pandas merge csv files - Axel_Erfurt - Dec-16-2019 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) RE: Pandas merge csv files - karlito - Dec-16-2019 (Dec-16-2019, 10:01 AM)Axel_Erfurt Wrote: Are the separators commas? yes they are commas! ok I would try your solution. |