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())
如果要水平合并CSV文件(添加列),请axis=1在调用函数时使用:pd.concat()
def merged_csv_horizontally(flist, **kwargs): return pd.concat([pd.read_csv(f, **kwargs) for f in flist], axis=1)