Home > Software engineering >  Merge dataframe from folder
Merge dataframe from folder

Time:10-04

I am trying to merge differents csv in Python. The files are in the same folder. All files have one column in common 'client_ID'. I tried this code:

path= r'/folder_path/'
allfiles = glob.glob(path   "/*.csv")
df = pd.DataFrame()

for file in allfiles:
    df_file = pd.read_csv(file)
    df_file = pd.merge(df, df_file, on='partner_id')
     
df

CodePudding user response:

You can read the first csv file first so that you don't start with an empty dataframe. I would edit your code like this:

path= r'/folder_path/'
allfiles = glob.glob(path   "/*.csv")

for i, file in enumerate(allfiles):
    if i < 1:
        df = pd.read_csv(file)
    else:
        df_file = pd.read_csv(file)
        df = pd.merge(df, df_file, on='partner_id')
 
df
  • Related