Saving multiple dataframes to CSV using loop in python

I am trying to save multiple dataframes to csv in a loop using pandas, while keeping the name of the dataframe.

import pandas as pd
 
df1 = pd.DataFrame({'Col1':range(1,5), 'Col2':range(6,10)})
df2 = pd.DataFrame({'Col1':range(1,5), 'Col2':range(11,15)})

frames = [df1,df2]

for data in frames:
    data['New'] = data['Col1']+data['Col2']
    
for data in frames:
    data.to_csv('C:/Users/User/Desktop/{}.csv'.format(data))

This doesn’t work, but the outcome I am looking for is for both dataframes to be saved in CSV format, to my desktop.

df1.csv
df2.csv

Thanks.

Answer

You just need to set the names of the CSV files; like so:

names = ["df1", "df2"]
for name, data in zip(names, frames):
    data.to_csv('C:/Users/User/Desktop/{}.csv'.format(name))