简体   繁体   中英

pandas join list elements to other dataframe column

I have the following pandas dataframes and I want to join the list column, look up and get the values.

df1 
x_id  y_id
1     [101,102,103]
2     [102, 103]
3     [103]

df2
y_id   y_name
101     abc
102     xyz
103     def

I want a dataframe df3 as:

df3
x_id  y_id             y_names
1     [101,102,103]    [abc, xyz, def]   
2     [102, 103]       [xyz, def]
3     [103]            [def]

Tried using lookup and apply functions but couldn't achieve.

A first option using a list comprehension and a mapping Series:

s = df2.set_index('y_id')['y_name']

df1['y_names'] = [[s[x] for x in l] for l in df1['y_id']]

A second option using explode , map and groupby.agg :

(df1
 .explode('y_id')
 .assign(y_names=lambda d: d['y_id'].map(df2.set_index('y_id')['y_name']))
 .groupby('x_id', as_index=False).agg(list)
)

output:

   x_id             y_id          y_names
0     1  [101, 102, 103]  [abc, xyz, def]
1     2       [102, 103]       [xyz, def]
2     3            [103]            [def]

used input:

df = pd.DataFrame({'x_id': [1, 2, 3],
                   'y_id': [[101, 102, 103], [102, 103], [103]]})

将 df2 转换为字典进行查找,然后使用数据框 apply 从字典中获取查找值

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM