Concatenating multiple csv files into a single csv with the same header - Python

后端 未结 4 733
萌比男神i
萌比男神i 2020-12-13 21:00

I am currently using the below code to import 6,000 csv files (with headers) and export them into a single csv file (with a single header row).

#import csv f         


        
4条回答
  •  旧时难觅i
    2020-12-13 21:34

    Here's a simpler approach - you can use pandas (though I am not sure how it will help with RAM usage)-

    import pandas as pd
    import glob
    
    path =r'data/US/market/merged_data'
    allFiles = glob.glob(path + "/*.csv")
    stockstats_data = pd.DataFrame()
    list_ = []
    
    for file_ in allFiles:
        df = pd.read_csv(file_)
        stockstats_data = pd.concat((df, stockstats_data), axis=0)
    

提交回复
热议问题