Home > Mobile >  How do I select based on a string another pandas dataframe in python?
How do I select based on a string another pandas dataframe in python?

Time:09-06

I have one main dataframe and several FX-Rate dataframes. The main one is loaded with data and with one column, which tells you the corresponding FX-Rate Table.

If i read one row in the main table, it tells me at the end: FX_TABLE_2 This comes as a string. How do I open than the FX_TABLE_2, which is a dataframe object?

e.g.

FX_TABLE_1 = pd.read_excel(path6, 'FX_TABLE_1',index_col=0)
FX_TABLE_2 = pd.read_excel(path6, 'FX_TABLE_2',index_col=0)
FX_TABLE_3 = pd.read_excel(path6, 'FX_TABLE_3',index_col=0)
FX_TABLE_4 = pd.read_excel(path6, 'FX_TABLE_4',index_col=0)
FX_TABLE_5 = pd.read_excel(path6, 'FX_TABLE_5',index_col=0)
FX_TABLE_6 = pd.read_excel(path6, 'FX_TABLE_6',index_col=0)
FX_TABLE_7 = pd.read_excel(path6, 'FX_TABLE_7',index_col=0)


for ind in Mainframe.index:
    ###### select FX Table for row
    FX_Table = Mainframe['FX_Table'][ind]
    print(type(FX_Table))
    print(type(FX_TABLE_5))

so i get a str for the first element, but based on this str out of the FX_Table column, i want to choose the correct dataframe. If i print the second element from the code, it is a <class 'pandas.core.frame.DataFrame'> and cannot be opened with a <class 'str'>.

Could anyone help? would be really nice!

It is basically select a dataframe from a output of a formula which is a str-element

CodePudding user response:

for instance this is the mainframe, but much more easier:

main = {
  "EXP": [1, 2, 3, 4],
  "EXP2": [1, 2, 3, 4],
  "FX_Table": ["FX_TABLE_5", "FX_TABLE_2","FX_TABLE_3","FX_TABLE_4"],
    "Currency": ["EUR", "EUR", "CNY", "CNY"]
}

#load data into a DataFrame object:
main = pd.DataFrame(main)

the fx-rates table for one year is also attached as imagefx_rates_table_example

so i want to read the mainframe, get the value of the column, e.g. FX_TABLE_2 --> then open the correct FX_TABLE-Dataframe like attached.

at the moment a get a STR-Element, but cannot open with this the correct dataframe, because it is another type.

CodePudding user response:

maybe this picture or structure of the process makes it more clear

process image

  • Related