I need to create lookup tables in python from a csv. I have to do this, though, by unique values in my columns. The example is attached. I have a name
column that is the name of the model. For reach model, I need a dictionary with the title from the variable
column, the key from the level
column and value from the value
column. I'm thinking the best thing is a dictionary of dictionaries. I will use this look up table in the future to multiply the values together based on the keys.
Here is code to generate sample data set:
Name = ['model1', 'model1', 'model1', 'model2', 'model2',
'model2','model1', 'model1', 'model1', 'model1', 'model2', 'model2',
'model2','model2']
Variable = ['channel_model','channel_model','channel_model','channel_model','channel_model','channel_model', 'driver_age', 'driver_age', 'driver_age', 'driver_age',
'driver_age', 'driver_age', 'driver_age', 'driver_age']
channel_Level = ['Dir', 'IA', 'EA','Dir', 'IA', 'EA', '21','22','23','24', '21','22','23','24']
Value = [1.11,1.18,1.002, 2.2, 2.5, 2.56, 1.1,1.2,1.3,1.4,2.1,2.2,2.3,2.4]
df= {'Name': Name, 'Variable': Variable, 'Level': channel_Level, 'Value':Value}
factor_table = pd.DataFrame(df)
I have read the following but it hasn't yielded great results: Python Creating Dictionary from excel data
I've also tried:
import pandas as pd
factor_table = pd.read_excel('...\\factor_table_example.xlsx')
#define function to be used multiple times
def factor_tables(file, model_column, variable_column, level_column, value_column):
for i in file[model_column]:
for row in file[variable_column]:
lookup = {}
lookup = dict(zip(file[level_column], file[value,column]))
This yields the error: `dict expected at most 1 arguments, got 2
What I would ultimately like is:
{{'model2':{'channel':{'EA':1.002, 'IA': 1.18, 'DIR': 1.11}}}, {'model1'::{'channel':{'EA':1.86, 'IA': 1.66, 'DIR': 1.64}}}}