import os
import glob
import pandas as pd
def get_merged_csv(flist, **kwargs):
return pd.concat([pd.read_csv(f, **kwargs) for f in flist], ignore_index=True)
path = 'C:/Users/csvfiles'
fmask = os.path.join(path, '*mask*.csv')
df = get_merged_csv(glob.glob(fmask), index_col=None, usecols=['col1', 'col3'])
print(df.head())
If you want to merge CSV files horizontally (adding columns), use axis=1
when calling pd.concat()
function:
def merged_csv_horizontally(flist, **kwargs):
return pd.concat([pd.read_csv(f, **kwargs) for f in flist], axis=1)