Home > Enterprise >  What's the most efficient way to export multiple pandas dataframes to csv files?
What's the most efficient way to export multiple pandas dataframes to csv files?

Time:09-23

I have multiple pandas dataframes:

df1
df2
df3

And I want to export them all to csv files.

df1.to_csv('df1.csv', index = False)
df2.to_csv('df2.csv', index = False)
df3.to_csv('df3.csv', index = False)

What's the most efficient way to do this?

def printToCSV(number):
    num = str(num)
    csvTemp = "df99.csv"
    csv = csvTemp.replace('99',num)
    dfTemp = "df99"
    dfString = dfTemp.replace('99',num)
    #i know i cant use the .to_csv function on a string
    #but i dont know how iterate through different dataframes
    dfString.to_csv(csv, index = False)

for i in range(1,4):
    printToCSV(i)

How can I can call different dataframes to export?

CodePudding user response:

You can add them all to a list with:

list_of_dataframes = [df1, df2, df3]

Then you can iterate through the list using enumerate to keep a going count of which dataframe you are writing, using f-strings for the filenames:

for count, dataframe in enumerate(list_of_dataframes):
    dataframe.to_csv(f"dataframe_{count}.csv", index=False)

CodePudding user response:

When you are creating the dataframes you can store them in a suitable data structure like list and when you want to create csv you can use map and do the same.

dfs = []
for i in range(10):
    dfs.append(DataFrame())

result = [dfs[i].to_csv(f'df{i}') for i in range(10)]

CodePudding user response:

If you want to stick with your function approach, use:

df_dict = {"df1": df1, "df2": df2, "df3": df3}


def printToCSV(num):
    df_dict[f"df{num}"].to_csv(f"df{num}.csv", index=False)

printToCSV(1) # will create "df1.csv"

However, if you want to increase efficiency, I'd propose to use a df list (as @vtasca proposed as well):

df_list = [df1, df2, df3]

for num, df in enumerate(df_list): # this will give you a number (starting from 0) and the df, in each loop
    df.to_csv(f"df{num}.csv", index=False)

Or working with a dict:

df_dict = {"df1": df1, "df2": df2, "df3": df3}

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