Yog
Yog

Reputation: 825

How to merge different csv files in a folder into a single csv file?

I have 40 csv files in a folder and I need to convert into a single csv file? The problem I face is though the body of csv's are the same the headings are different across sheets how do I correct them and merge?

Upvotes: 2

Views: 885

Answers (1)

Mohamed Thasin ah
Mohamed Thasin ah

Reputation: 11192

try this,

read the files without header, by default it will put range(length_of_the_columns). then concat your df's finally create your header.

import os
import pandas as pd

df=pd.DataFrame()
for file_ in os.listdir(folder_path):
    temp=pd.read_csv(folder_path+'/'+file_,header=None,skiprows=1)
    df=pd.concat([df,temp],ignore_index=True)

df.columns=[new_column_names]
df.to_csv('single_file.csv',index=False)

Upvotes: 3

Related Questions