Home > other >  Match the values of two dataframes and return a dictionary
Match the values of two dataframes and return a dictionary

Time:12-06

I have a dataframe like this

df1 = pd.DataFrame(
       {   
            "index":    ["December",  "December",  "November", "November"],
            "index1":   ["285",       "296",       "397",      "300"]})


     index     index1
0   December    285
1   December    296
2   November    397
3   November    300 

And also o second dataframe like this

df2 = pd.DataFrame(
    {   
        "index":     ["December",  "December",  "November", "November"],
        "index1":    ["285",       "296",       "397",      "300"],
        "eventid":   ["67511",     "67512",     "64515",    "64517"]})


     index     index1    eventid
0   December    285       67511
1   December    296       67512
2   November    397       64515
3   November    300       64517

I want to match index & index1 of df1 and index & index1 of df2 and return the eventid of df2 in a dictionary

For example:

For the first row of df1 % df2:

0 December 285 of df1 matches with 0 December 285 of df2 so the dictionary will be

{285: 67511}

For the second row of df1 % df2:

1 December 296 of df1 matches with 1 December 296 of df2 so the dictionary will be

{285: 67511, 296:67512}

The first and second rows go together as they have the same index value (i.e December).

And so on for the other rows

Any ideas

CodePudding user response:

Let us merge the dataframes then group the resulting dataframe by index and create the record for each unique index inside a dict comprehension

{k: dict(zip(g['index1'], g['eventid'])) 
    for k, g in df2.merge(df1).groupby('index')}

{'December': {'285': '67511', '296': '67512'},
 'November': {'397': '64515', '300': '64517'}}
  • Related