0

I have two dataframes namely a_df and b_df and have defined them separately under two different if conditions.

Once, outside the conditions I want to use them like {i}_df['col'] where the value is input by user and can be a, or b. How do I incorporate a variable into the name of dataframe as such.

Ynjxsjmh
  • 28,441
  • 6
  • 34
  • 52
new2cod3
  • 31
  • 5

1 Answers1

0

I think that the a valuable solution in this case is to use a dictionary for mapping the user input with the actual dataframe. For example:

my_dict={'a':a_df,'b':b_df}

Then, you select the correct element according to the provided input. Something like the following:

user_input=input('Your choice: ')

try:
    print(my_dict[user_input])
except:
    print("Not valid!")

Hope it helps!