0
votes

I'm web-scraping the data from a website, so I created a for loop to collect the data from multiple webpages and generate separate dataframes. However, I don't know how to save them with different names.

I first created the list of dataframe names:

dataframe_names=[]
for i in range(0,50):
    text='item'+str(i)
    dataframe_names.append(text)

I then included the following code in the loop that creates dataframes:

df=[name for name in dataframe_names]

The expected output is the dataframes created from for loop and saved all separately such as df1, df2,df3,.....df50.

However it didn't work. Could you help solve this issue?

1
Can you edit your question and put there expected output? - Andrej Kesely
you can loop over your dataframe by using a groupby then save the outputs i.e for group, data in df.groupby(group_by_key):;data.to_csv(f"{group}.csv",index=False) better if you add a subset of your data - Umar.H
@AndrejKesely I just did. - Cielo Kim
@Manakin Thanks a lot. When I would like to save each dataframes in each variable, what code should I write? - Cielo Kim

1 Answers

0
votes

I haven't figured out how to assign names for dataframes created from for loop, but I fixed the problem by doing the following.

  1. Create the empty list before for loop creating multiple dataframes. This list will be used to append the dataframes to be newly created from for loop.

    list=[]

  2. Inside the for loop, after code generating df, add this code.

    for loop: # creating multiple dataframe list.append(df)

  3. Make multiple dataframes into one using pd.concat

    data=pd.concat(list) data.to_csv('data.csv', index=False)