0
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.

2
  • You can read manual of pandas concat which is what you are looking for here: pandas.pydata.org/docs/reference/api/pandas.concat.html Commented Jun 12, 2022 at 9:38
  • Prefer to use concat once at the end instead of once per loop iteration - it will save time & memory. Commented Jun 12, 2022 at 9:50

2 Answers 2

1

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'])
Sign up to request clarification or add additional context in comments.

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.