DECROMAX

concat multiple csv within dir with pathlib

Mar 5th, 2022
361
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.39 KB | None | 0 0
  1. from pathlib import Path
  2. import pandas as pd
  3.  
  4. # define target directory
  5. csv_dir = Path.home().joinpath("foo", "bar").glob("*.csv")
  6.  
  7. # loop through dir & append df's to list (use additional param in read_csv if req)
  8. df_list = []
  9. for i in list(csv_dir):
  10.     print(i)
  11.     df_list.append(pd.read_csv(i))
  12.    
  13. # concat df on row axis (axis=0)
  14. df = pd.concat(df_list, axis=0, ignore_index=True)
Advertisement
Add Comment
Please, Sign In to add comment