Loop over multiple .csv files python/pandas -
i have 2 folders contain +50 .csv files, want process al files in python code pandas. @ beginning of code load 2 different .csv files:
location1 = path\tasks_01.csv' location2 = path\resource_01.csv' dftask = pd.read_csv(location1) dfresource = pd.read_csv(location2)
in middle kind of different operations structure data etc. @ end save both .csv files new .csv file:
dftask.to_csv(path\tasks_new.csv') dfresource.to_csv(path\resource_new.csv')
since have 2 folders, 1 containts task.csv files , other resource.csv files how can edit code in such way can loop on files? , save them under original name?
hope can me out!
create list of files in each folder , zip through both of them.
import os files_in_folder_1 = [os.path.join(path1, f) f in os.listdir(path1) if os.path.isfile(os.path.join(path1, f))] files_in_folder_2 = [os.path.join(path2, f) f in os.listdir(path2) if os.path.isfile(os.path.join(path2, f))] file1, file2 in zip(files_in_folder_1, files_in_folder_2): open(file1) f1, open(file2) f2: ...
Comments
Post a Comment