5

I had a problem, which is a for loop program.like below:

list = [1,2,3,4]

for index in list:
    new_df_name = "user_" + index
    new_df_name = origin_df1.join(origin_df2,'id','left')

but the "new_df_name" is just a Variable and String type.

how to realize these?

Frank
  • 977
  • 3
  • 14
  • 35
  • If you're trying to append `index` to `user_`, try this: `new_df_name = "user_" + str(index)` – Sivaprasanna Sethuraman May 11 '18 at 04:40
  • But dataframe join cannot return a string type name. new_df_name_df = "user_" + region new_df_name_df = user_profile_region.join(shopee_backend_user_df, 'userid', 'left') it cannot be run. – Frank May 11 '18 at 05:57
  • You could use a list that you append the dataframes to and then reduce it to a single one. Or you could create the result_df iterativly in the loop. – Shaido May 11 '18 at 07:59
  • Possible duplicate of [How do I create a variable number of variables?](https://stackoverflow.com/questions/1373164/how-do-i-create-a-variable-number-of-variables) – Alper t. Turker May 11 '18 at 09:08

1 Answers1

0

I assume, what you really need is to have a list of dataframes (which non necessary have any specific names) and then union them all together.

dataframes = [df1, df2, df3, etc... ]

res_df, tail_dfs = dataframes[0], dataframes[1:]
for df in tail_dfs:
  res_df = res_df.unionAll(df)

upd. even better option to union described in comment.

vvg
  • 6,325
  • 19
  • 36