0

I got the following for loop in Python:

for x in ["a","b","c"]:

Inside this loop, I'm defining a data frame that includes a, b, and c in their name: df_a, df_b, df_c. I also need those a, b, and c for calculations, say, a+1, etc.

At the end of this loop, I want to print the number of observations in each data frame: df_a.shape[0], etc.

Can anyone address me on how to do this?

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
Saber
  • 1
  • 2
  • 1
    Don't. Use a dictionary to index your objects, not variable names. – mozway Aug 18 '22 at 19:48
  • `dfs = [df_a, df_b, df_c, df_d]`, then `for x in dfs:`. – Tim Roberts Aug 18 '22 at 19:55
  • `dictionary = {'a': df_a, 'b': df_b, 'c': df_c} # map names with dataframes` `for name, df in dictionary.items():` `print("name :" + name)` `print("Dataframe length =" + df.shape[0])` – Henry8 Aug 19 '22 at 04:49

0 Answers0