I have a code written using for loop to save the dataframes present in a list (Date is the name of the list) to the specified path
for Dates in Date:
if Dates.empty:
pass
else:
PATH = f'C:/Users/Desktop/' Dates.iloc[0]['Col1'] '/' Dates.iloc[0]['Col2'] '/'
if not os.path.exists(PATH):
os.makedirs(PATH)
Day = Dates.iloc[0]["DAY"]
Dates = Dates.drop(['Col1', 'Col2'], axis=1)
Dates.to_csv(os.path.join(PATH,f'{Day}.csv'),index=False)
However, this code executes for a long time. Can anyone please help me how to modify the above code using map function to reduce the time?
CodePudding user response:
Under the assumption that your for loop works correctly you could use a map function as follow:
import pandas as pd
def save_dataframe(Dates: pd.DataFrame):
if not Dates.empty:
PATH = f'C:/Users/Desktop/' Dates.iloc[0]['Col1'] '/' Dates.iloc[0]['Col2'] '/'
if not os.path.exists(PATH):
os.makedirs(PATH)
Day = Dates.iloc[0]["DAY"]
Dates = Dates.drop(['Col1', 'Col2'], axis=1)
Dates.to_csv(os.path.join(PATH,f'{Day}.csv'),index=False)
my_map = map(save_dataframe, Date) # generation of your map
list(my_map) # excutions of the map