0

I'm trying to recreate what I achieved manually using a for loop What I did was to manually written 10 CSV files. the code is like this

     df_1.to_csv('eblist1.csv', encoding='latin-1', index=False)
     df_2.to_csv('eblist2.csv', encoding='latin-1', index=False)
     df_3.to_csv('eblist3.csv', encoding='latin-1', index=False)
     df_4.to_csv('eblist4.csv', encoding='latin-1', index=False)
     df_5.to_csv('eblist5.csv', encoding='latin-1', index=False)
     df_6.to_csv('eblist6.csv', encoding='latin-1', index=False)
     df_7.to_csv('eblist7.csv', encoding='latin-1', index=False)
     df_8.to_csv('eblist8.csv', encoding='latin-1', index=False)
     df_9.to_csv('eblist9.csv', encoding='latin-1', index=False)
     df_10.to_csv('eblist10.csv', encoding='latin-1', index=False)

That works fine as expected it writes all these files from my dataframes. Then I tried to achieve the same thing using a loop ... and here is where I got stuck, I spent hours reading at different solutions, and tried several options, but I always end up getting a syntax error

     line 98, in <module>
names.to_csv('eblist'+str(res)+'.csv', encoding='latin-1', index=False)

     AttributeError: 'str' object has no attribute 'to_csv'

The only thing I need is a way to add the value to the string, as the loop iterates through the dictionary, and believe me, I tried many approaches ...

      d = {'df_1':df_1,'df_2': df_2,'df_3': df_3,'df_4': df_4,'df_5': 
      df_5,'df_6': df_6,'df_7': df_7,'df_8': df_8,'df_9': df_9,'df_10': 
      df_10}

      for names in d:
          res = list(d.keys()).index(names) 
          names.to_csv('eblist'+str(res)+'.csv', encoding='latin-1', index=False)
1
  • my guess is that you don't even need to have this many dataframes. they should all be in one dataframe with an identifying column and a bunch of groupby operations Commented Feb 13, 2021 at 1:21

2 Answers 2

1

Based on the error message "'str' object has no attribute 'to_csv'", it appears you attempted to access the to_csv() method from a string and not the class itself. You are on the right track. Assuming you have a list of your dataframes (the actual dataframes and not strings of the dataframes), then you should be able to do what you are looking for:

  dfs = [df_1, df_2, df_3, df_4, df_5, df_6, df_7, df_8, df_9, df_10]

  for index,df in enumerate(dfs,1):
      df.to_csv(f'eblist{index}.csv', encoding='latin-1', index=False)
Sign up to request clarification or add additional context in comments.

4 Comments

improvement suggestion: enumerate(dfs, 1) to get rid of the index+1 clutter inside the loop-body?
@StefanB Nice!!
@Bobby Ocean Thank you! My limited Python knowledge shows ;) ... I upvoted your answer, that should do the trick!
also thanks to @StefanB for improving it!
0
AttributeError: 'str' object has no attribute 'to_csv'

This is due to your loop:

for names in d:

This is iterating over the keys of your dictionary, which are strings. You actually never access to the values of your dictionary (the dataframes). If you really want to use a dictionary, just do:

for names, df in d.items():
    res = list(d.keys()).index(names) 
    df.to_csv('eblist'+str(res)+'.csv', encoding='latin-1', index=False)

Or better use a list instead of a dictionary as suggested in other answers, since it seems that you are interested in the position and the keys do not have any other meaning.

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.