0

I have several data frames: df_1, df_2, df_3, df_4, etc.

I am trying to loop through them, perform an operation and output the name of the dataframe rather than the dataframe itself.

for k in [df_1, df_2, df_3, df_4]:
    k.RandomField.value_counts() #or whatever operations
    print(k) #this is where I want it to show "df_1", "df_2", etc instead of printing the variable which, in this case, would be the data frame

I know I can create a dictionary but that requires manually generating one. Appreciate your help.

[I am working in Jupyter notebooks and running Python 3.8.]

  • Does this answer your question? [Getting the name of a variable as a string](https://stackoverflow.com/questions/18425225/getting-the-name-of-a-variable-as-a-string) – Derek O Sep 24 '21 at 15:14
  • But you have to manually generate the list anyway, so why is it any different? If you can populate the list, you can populate the dict – ALollz Sep 24 '21 at 15:14
  • This is a good example why one should never use variables when working with many objects, but a **container**. How did you generate the variables? Go back from there and store the values in a list/dictionary. – mozway Sep 24 '21 at 15:14

0 Answers0