Advertisement
skip420

combineCSVFiles

Sep 8th, 2022
835
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.54 KB | None | 0 0
  1. #python3 combineCSVFiles.py
  2.  
  3.  
  4.  
  5.  
  6. import os
  7. import glob
  8. import pandas as pd
  9. #set working directory
  10. os.chdir("~/Desktop")
  11.  
  12. #find all csv files in the folder
  13. #use glob pattern matching -> extension = 'csv'
  14. #save result in list -> all_filenames
  15. extension = 'csv'
  16. all_filenames = [i for i in glob.glob('*.{}'.format(extension))]
  17. #print(all_filenames)
  18.  
  19. #combine all files in the list
  20. combined_csv = pd.concat([pd.read_csv(f) for f in all_filenames ])
  21. #export to csv
  22. combined_csv.to_csv( "combined_csv.csv", index=False, encoding='utf-8-sig')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement