Aviral Harsh
Aviral Harsh

Reputation: 1

Merging multiple csv files(unnamed colums) from a folder in python

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.

Upvotes: 0

Views: 637

Answers (2)

gtomer
gtomer

Reputation: 6564

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']

Upvotes: 0

AKHacks
AKHacks

Reputation: 207

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'])

Upvotes: 1

Related Questions