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)
groupbyoperations