Search code examples
pythonpython-3.xlistcsvexport-to-csv

How do i export a list of dataframes to each csv file?


I have a list of dataframes and i want to export each element in different csv file. How can i do it? I'm newbie in python. Thanks in advance

col1 col2
a    b
c    d

df1 above

col3 col4
e    f
g    h

df2 above

list = [df1, df2]

Solution

  • You can do this by iterating over the list of dataframes and their names at the same time. The following code should work

    dfs = [df1, df2]
    names = ['df1', 'df2']
    
    for df,name in zip(dfs, names): 
       df.to_csv(name + '.csv')