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?
eg
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
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 image fx_rates_table_example
so i want to read the mainframe, get the value of the column, eg 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.
maybe this picture or structure of the process makes it more clear
Ok, so it looks like you have quite a misconception here.
Please note, you cannot load dataframe into another as they are separate objects, however, you can load data from one df into another in different ways, using concat
, merge
and etc.
small example for you below, I hope it will lead you forward, it has comments of each step:
main = {
"EXP": [1, 2, 3, 4],
"EXP2": [1, 2, 3, 4],
"FX_Table": ["FX_TABLE_1", "FX_TABLE_2","FX_TABLE_1","FX_TABLE_2"],
"Currency": ["EUR", "EUR", "CNY", "GBP"]
}
#load data into a DataFrame object:
main = pd.DataFrame(main)
path6 = r'C:\Sandbox\test.xlsx'
Excel_File = pd.ExcelFile(path6) # reads whole excelFile at once
dfs_dict = {sh:Excel_File.parse(sh) for sh in Excel_File.sheet_names} # loading each sheet to DataFrame and each dataframe to dictionary
print(dfs_dict.keys()) # will show each sheet name loaded
print(dfs_dict['FX_TABLE_2']) # will show data from single DataFrame by it's key in the dictionary (which is the same as sheet name)
#adding new column to each DataFrame saved in the dictionary:
for key in dfs_dict.keys():
dfs_dict[key]['Sheet_Name'] = key
print(dfs_dict['FX_TABLE_2']) # will show DataFrame with new column which can be used as unique ID
# now you can add data from other tables into Main or New_main dataframe, - up to you. I will use Sheet Name and Currencty as unique ID for merging
New_main = pd.merge(main, dfs_dict['FX_TABLE_1'], how='outer', left_on=['FX_Table', 'Currency'], right_on=['Sheet_Name','currency'])
print(New_main)
EXP EXP2 FX_Table Currency currency ... 2008 2009 2010 2011 Sheet_Name
0 1 1 FX_TABLE_1 EUR EUR ... 3.0 3.05 3.1 3.15 FX_TABLE_1
1 2 2 FX_TABLE_2 EUR NaN ... NaN NaN NaN NaN NaN
2 3 3 FX_TABLE_1 CNY CNY ... 2.0 2.05 2.1 2.15 FX_TABLE_1
3 4 4 FX_TABLE_2 GBP NaN ... NaN NaN NaN NaN NaN
or as per your comment:
FX_Tab_list = main['FX_Table'].to_list # load table names to the list from Main DF and loop the list
for table in FX_Tab_list:
dfs_dict[table] = your code lands here
#for example:
dfs_dict[table]['difference'] = dfs_dict[table]['2009'] - dfs_dict[table]['2008']
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.