3

I have three different Pandas dataframes

df_1
df_2
df_3

I would like to loop over the dataframes, do some computations, and store the output using the name of the dataframe. In other words, something like this

for my_df in [df_1, df_2, df_3]:
  my_df.reset_index(inplace=True)
  my_df.to_csv('mypath/' + my_df +'.csv')

Output files expected:

'mypath/df_1.csv', 'mypath/df_2.csv' and 'mypath/df_3.csv'

I am struggling doing so because df_1 is an object, and not a string. Any ideas how to do that?

Thanks!

2 Answers 2

5

Another more general solution is create dict with column names and then loop by items():

d = {'df_1':df_1,'df_2':df_3, 'df_3':df_3}
for k, my_df in d.items():
    my_df.reset_index(inplace=True)
    my_df.to_csv('mypath/' + k +'.csv')

Another possible solution is use another list with dataframes names, then enumerate and get value of name by position:

names = ['a','b','c']
print ({names[i]: df for i, df in enumerate([df_1, df_2, df_3])})
Sign up to request clarification or add additional context in comments.

9 Comments

Use dict comprehension for mapping ;)
@jezrael dict comprehension you mean?
Yes, exactly, sorry.
I am not python expert, but I think if yes, so not easy.
@Noobie The object pointed by jezrael_the_pythonista may have a lot of other names so it is not actually possible to do that.
|
1

To store df as csv using to_csv method we need a string path.

So we enumerate over the list. This gives us 2 variable in the for loop. 1st variable is the index of the loop iteration. It's like a counter.

So basically enumerate gives us a counter object on the loop.

We are using the counter value to create a string of the index. Which we use to create a unique file name to store.

for idx, my_df in enumerate([df_1, df_2, df_3]):
  my_df.reset_index(inplace=True)
  my_df.to_csv('mypath/df_' + str(idx + 1) +'.csv')

1 Comment

This names them df_0, df_1, df_2 and makes assumptions that the names of the dataframes always fits that pattern. To get this pattern correct at least update the answer with enumerate(enumerate([df_1, df_2, df_3], 1)

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.