-2

I have a folder that has multiple .csv files. I'm trying to loop through each of the csv file,pick certain columns and merge it with an already existing file. can anyone help me in this regard......

  • What exactly was the problem when you tried to do this? – mkrieger1 Aug 25 '20 at 12:39
  • 1
    Does this answer your question? [Import multiple csv files into pandas and concatenate into one DataFrame](https://stackoverflow.com/questions/20906474/import-multiple-csv-files-into-pandas-and-concatenate-into-one-dataframe) – Newbie_2006 Aug 25 '20 at 12:40

1 Answers1

0

try this:

If you have same columns in all your csv files then you can try the code below. I have added header=0 so that after reading csv first row can be assigned as the column names.

import pandas as pd
import glob

path = r'C:\TEST' # use your path
all_files = glob.glob(path + "/*.csv")

li = []

for filename in all_files:
    df = pd.read_csv(filename, index_col=None, header=0)
    li.append(df)

frame = pd.concat(li, axis=0, ignore_index=True)

Check this answer for more information: Import multiple csv files into pandas and concatenate into one DataFrame

Jean Camargo
  • 340
  • 3
  • 17