table = pd.DataFrame(data=[[1,2,3],[4,5,6],[7,8,9]],
columns=['High','Middle','Low'],
index=['Blue','Green','Red'])
df = pd.DataFrame(data=[['High','Blue'],
['High','Green'],
['Low','Red'],
['Middle','Blue'],
['Low','Blue'],
['Low','Red']],
columns=['A','B'])
>>> df
A B
0 High Blue
1 High Green
2 Low Red
3 Middle Blue
4 Low Blue
5 Low Red
>>> table
High Middle Low
Blue 1 2 3
Green 4 5 6
Red 7 8 9
I'm trying to add a third column 'C' which is based on the values in the table. So the first row would get a value of 1, the second of 4 etc.
If this would be be a one-dimensional lookup I would convert the table to a dictionary and would use df['C'] = df['A'].map(table). However since this is two-dimensional I can't figure out how to use map or apply.
Ideally I would convert the table to dictionary format so I save it together with other dictionaries in a json, however this is not essential.
tablevalues to categorical pairs indfwhy don't you try the following:table.unstack().reset_index(). This will melt your table down into the 9 values you want to manipulate.