如何在熊猫数据框中解压缩列表列



我有一个形式的pandas DataFrame

age    begin        end       product_id
0   40  1578178800  1579388400        [3, 4]
1   30  1578178800  1579388400   [3, 4, 6, 2, 5, 1]
2   30  1578178800  1578265200         [6]
3   58  1578178800  1578265200         [6]
4   30  1578178800  1578265200         [6]

因为theproduct_id是客户选择的产品的组合。例如:1 - 电子产品,2 - 汽车,3- 配件,4 - 机器人,5 - 培训,6 - 其他

我想在下面的表格中DataFrame,周围没有列表:

age    begin        end                         product_id
0   40  1578178800  1579388400                 accessories, Robots
1   30  1578178800  1579388400   accessories, Robots, Other, automobiles, Training, electronics
2   30  1578178800  1578265200                       Other
3   58  1578178800  1578265200                       Other
4   30  1578178800  1578265200                       Other

按字典对映射值使用列表推导,并转换为连接字符串:

d = {1:'electronics',
2: 'automobiles',
3:'accessories', 
4:'Robots',
5:'Training',
6: 'Other'}
df['product_id'] = df['product_id'].map(lambda x: ', '.join(d[y] for y in x))
print (df)
age       begin         end  
0   40  1578178800  1579388400   
1   30  1578178800  1579388400   
2   30  1578178800  1578265200   
3   58  1578178800  1578265200   
4   30  1578178800  1578265200   
product_id  
0                                accessories, Robots  
1  accessories, Robots, Other, automobiles, Train...  
2                                              Other  
3                                              Other  
4                                              Other  

或:

df['product_id'] = [', '.join(d[y] for y in x) for x in df['product_id']]

最新更新