Home > OS >  Get dict keys using pandas apply
Get dict keys using pandas apply

Time:06-03

i want to get values from the dict that looks like

pair_devices_count = 
{('tWAAAA.jg', 'ttNggB.jg'): 1,
('tWAAAM.jg', 'ttWVsM.jg'): 2,
('tWAAAN.CV', 'ttNggB.AS'): 1,
 ('tWAAAN.CV', 'ttNggB.CV'): 2,
 ('tWAAAN.CV', 'ttNggB.QG'): 1}

(Pairs of domain)

But when i use

train_data[['domain', 'target_domain']].apply(lambda x: pair_devices_count.get((x), 0))

it raises an error, because pandas series are not hashable How can i get dict values to generate column train['pair_devices_count']?

CodePudding user response:

you cannot apply on multiple columns. You can try this :

train_data.apply(lambda x: pair_devices_count[(x.domain, x.target_domain)], axis=1)

CodePudding user response:

pandas series are not hashable

Convert pd.Series to tuple before using .get consider following simple example

import pandas as pd
d = {('A','A'):1,('A','B'):2,('A','C'):3}
df = pd.DataFrame({'X':['A','A','A'],'Y':['C','B','A'],'Z':['X','Y','Z']})
df['d'] = df[['X','Y']].apply(lambda x:d.get(tuple(x)),axis=1)
print(df)

output

   X  Y  Z  d
0  A  C  X  3
1  A  B  Y  2
2  A  A  Z  1
  • Related