2

I am reading specific csv files names based on their name with this code:

csv_names = [s for s in files_csv if "drive" in s]

output:

['drive_1.csv',
 'drive_2.csv',
 'drive_3.csv',
 'drive_4.csv']

How can i merge those files easy?

I am know doing this:

df = pd.read_csv(csv_names[0], sep=';', header=None) 
df1 = pd.read_csv(csv_names[1], sep=';', header=None)
df2 = pd.read_csv(csv_names[2], sep=';', header=None)
df3 = pd.read_csv(csv_names[3], sep=';', header=None)

I don't want to read them one by one. And then merge them. I hope someone has a great idea.

2
  • 1
    Do you want to merge the files or append them? I have a feeling you want to append the files together. If so, then df = pd.concat([pd.read_csv(file, sep=';', header=None) for file in csv_names]) Commented Jun 29, 2021 at 14:09
  • Does this answer your question? how to merge 200 csv files in Python Commented Jun 29, 2021 at 14:26

2 Answers 2

1

You can use pd.concat and a list comprehension:

df = pd.concat([pd.read_csv(csv_name, sep=';', header=None) for csv_name in csv_names])
Sign up to request clarification or add additional context in comments.

Comments

0
import pandas as pd
df = pd.DataFrame()
for csv in csv_name:
    df = pd.concat([df, pd.read_csv(csv, sep=';', header=None)])
df.to_csv('Merged_Data_Frame.csv', index=False)

Hope this helps. This will merge and save them to csv for later use.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.