0

I want to create a function, which looks like the following:

def function(ticker,A,B):
    dataframe = df['string']
    return dataframe

The dataframe df should get the Strings like '20' oder '10' so that its name looks like, e.g. df_20_10.

def function(ticker,A,B):
        dataframe = df_A_B['string']
        return dataframe

-> does not work

Can anybody help me, please?

John Kugelman
  • 349,597
  • 67
  • 533
  • 578
Tom
  • 311
  • 2
  • 5
  • 9
  • 2
    Variable names shouldn't be data. You should probably create a dictionary or list of your dataframes, which you can index with `A` and `B` to get the particular one you want. – Blckknght Jan 26 '18 at 19:31

2 Answers2

0

You could do this using locals() or globals(), like this:

dataframe = locals()['df_{}_{}'.format(A, B)]['string']

Shorter version (in Python 3.6+):

dataframe = locals()[f'df_{A}_{B}']['string']
Omar Einea
  • 2,478
  • 7
  • 23
  • 35
0
def function (ticker,A,B):
    dataframe = eval("df_"+A+"_"+B)["string"]
    return dataframe

I'm not sure if this is what you want though. Sorry if it's not.

Dan
  • 527
  • 4
  • 16