Home > front end >  Merging multiple csv files(unnamed colums) from a folder in python
Merging multiple csv files(unnamed colums) from a folder in python

Time:06-12

import pandas as pd
import os
import glob

path = r'C:\Users\avira\Desktop\CC\SAIL\Merging\CISF'

files = glob.glob(os.path.join(path, '*.csv'))

combined_data = pd.DataFrame()


for file in files :
    
    data = pd.read_csv(file)
    print(data)
    
    combined_data = pd.concat([combined_data,data],axis=0,ignore_index=True)
    
    
combined_data.to_csv(r'C:\Users\avira\Desktop\CC\SAIL\Merging\CISF\data2.csv')

The files are merging diagonally,ie-next to the last cell of the first file, is the beginning of second file. ALSO, it is taking the first entry of file as column names. All of my files are without column names. How do I vertically merge my files,and provide coluumn names to the merged csv.

CodePudding user response:

For the header problem while reading csv , u can do this:

pd.read_csv(file, header=None)

While dumping the result u can pass list containing the header names

df.to_csv(file_name,header=['col1','col2'])

CodePudding user response:

You need to read the csv with no headers and concat:

data = pd.read_csv(file, header=None)
combined_data = pd.concat([combined_data, data], ignore_index=True)

If you want to give the columns meaningful names:

combined_data.columns = ['name1', 'name2', 'name3']
  • Related