Home > Mobile >  How best to export each dataframe in a dictionary of dataframes to a csv?
How best to export each dataframe in a dictionary of dataframes to a csv?

Time:04-09

I have a dictionary of dataframes, and I want to export each dataframe within my dictionary to a csv, and I'm wondering about how best to do this. I've executed the following code to get my dictionary:

df_dict = pd.read_excel(r'My File Path for Excel Sheet', sheet_name=None, header=0, index_col=0)

So that all of my sheets are in one dictionary, with each sheet as a key-value pair. To be clear, I'd like one csv file for each key-value pair (i.e. each dataframe) I'm not sure if this is best accomplished via a for loop, or what, but any advice and/or insight would be appreciated, and I'm happy to provide more context as needed.

Thanks!

CodePudding user response:

a simple for loop should do it. I'm sure you could make it more efficent with threading and the sort, but not sure it's worth it!

df_dict = pd.read_excel(r'My File Path for Excel Sheet', sheet_name=None, header=0, index_col=0)

for sheet, dataframe in df_dict.items():
    dataframe.to_csv(f"{sheet}.csv",index=False)
  • Related