1

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.

1
  • Which version of Python are you using? Commented Sep 25, 2021 at 21:14

3 Answers 3

5

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))
Sign up to request clarification or add additional context in comments.

6 Comments

Or using an f-string: data.to_csv(f'C:/Users/User/Desktop/{name}.csv')
yeah sure, but this won't work if he uses python 3.4 for example
Or in Python 2.7, if you want to choose a random Python version older than 3.9?
Thank you, has solved this issue.
@balmy haha, true!! I just wanted to point out that this feature was added in only python 3.5... any version before that won't work
|
0

Hope this help. Note I did not use the format function. But I set up code in the directory I am working on.

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']
  
n = 0
for data in frames:
    n = n + 1
    data.to_csv('df' + str(n) + ".csv")

1 Comment

np. Now it will be saved as df1.csv, df2.csv.... if you want it to be "Jack" change it to data.to_csv('Jack' + str(n) + ".csv") in the loop area.
0

In this loop:

for data in frames:
    data.to_csv('C:/Users/User/Desktop/{}.csv'.format(data))

You are looping over a list of DataFrame objects so you cannot use them in a string format.

Instead you could use enumerate() to get the indexes as well as the objects. Then you can use the indexes to format the string.

for idx,data in enumerate(frames):
    data.to_csv('df{}.csv'.format(idx + 1)) 
# the reason for adding 1 is to get the numbers to start from 1 instead of 0

Otherwise you can loop through your list just using the index like this:

for i in range(len(frames)):
    frames[i].to_csv('df{}.csv'.format(idx + 1)) 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.