Loop through multiple CSV files and run a script

后端 未结 4 1351
故里飘歌
故里飘歌 2021-01-28 16:40

I have a script which pulls in data from a csv file, does some manipulations to it and creates an output excel file. But, its a tedious process as I need to do it for multiple f

4条回答
  •  悲&欢浪女
    2021-01-28 17:15

    Something like:

    import os
    import glob
    import pandas as pd
    

    os.chdir(r'path\to\folder') #changes folder path to working dir
    filelist=glob.glob('*.csv') #creates a list of all csv files
    for file in filelist:       #loops through the files
        df=pd.read_csv(file,...)
        #Do something and create a final_df
        final_df.to_excel(file[:-4],+'_output.xlsx',index=False) #excel with same name+ouput
    

提交回复
热议问题