Skip to content Skip to sidebar Skip to footer

Python Pandas - Merge Csv Files In Directory Into One

I have a directory with csv files: frames/df1.csv df2.csv frames are structured like this: df1.csv artist track plays 1 Pearl Ja

Solution 1:

I just simply using your code create the list of DataFrame

path = 'frames'
all_files = glob.glob(path + "/*.csv")
l= []
for file_ in all_files:
    df = pd.read_csv(file_,index_col=None, header=0)
    l.append(df)

Then using functools.reduce, merge the list dataframe into one

import functools
l= [df1, df2, df3....]
merged_df = functools.reduce(lambda left,right: pd.merge(left,right,on=['artist','track']), l)

Solution 2:

DataFrame.join is useful. Its analogous to a SQL join. Something like:

df1.join(df2, on=('artist', 'track'))

Post a Comment for "Python Pandas - Merge Csv Files In Directory Into One"