I have a dictionary where the key is a name and the value is another dictionary. For example:
dict1 = {name1: {...}, name2: {...},...}
I also have a Pandas dataframe df
with columns 'A', and 'B'.
I would like to add a column to the dataframe using those column values as keys in dict1
.
For example, for each row the value in 'A' to be used as the key in dict1
and the value in 'B' to be used as the key to the inner dictionary, with the result being put in column C.
I am trying things like df[C] = dict1[df[A]][df[C]]
or df[C] = df[[A,B]].apply(lambda x,y: dict1[x][y])
but nothing seems to be working. Ideally I would like do this without looping through the rows without something like df.itertuples()
.
Example df row (A, B):
'key1' | 'key2'
I want the following (A, B, C):
'key1' | 'key2' | dict1['key1']['key2']