How to create a loop for changing data frame names

66 Views Asked by At

I have several data frames and for all of them, I want to carry out the same procedure. As an example, for one data frame (df1), the code looks the following:

def create_url():
    df_id = (str(str((df1['id'].tolist()))[1:-1])).replace(" ", "")
    ids1 = "ids=" + df_id1
    url1 = "https://api.twitter.com/2/tweets?{}&{}".format(ids1, tweet_fields)
    return url1

However, for all df which I have I want to loop this code in order to get one url per df (which are names then url1, url2, etc.). To get the whole list of names of df, I used the following code:

for j in dfs:
    print(j)

Hope somebody can help!

Thank you a lot in advance!

1

There are 1 best solutions below

0
dimelu On

If i understand correctly
Try this:

def create_url(dfx):
    df_id = (str(str((dfx['id'].tolist()))[1:-1])).replace(" ", "")
    ids = "ids=" + df_id
    url = "https://api.twitter.com/2/tweets?{}&{}".format(ids, tweet_fields)
    return url


for j in dfs:
    print(j)
    create_url(j)