Python 如何在dataframe中解压缩列表列

Python 如何在dataframe中解压缩列表列,python,python-3.x,pandas,dataframe,Python,Python 3.x,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

我有一个
熊猫数据框
,格式如下:

    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]
因为产品id是客户选择的产品的组合。例如:1-电子,2-汽车,3-配件,4-机器人,5-培训,6-其他

我想要
数据框
的格式如下,但不包括其周围的列表:

    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  
或:


@微笑谢谢。我得到了错误:KeyError:'3'可能有什么问题?@Pythonista-这意味着
3
不是整数,而是字符串
'3'
,所以尝试将
d={1:'electronics',2:'automobiles',3:'accessories',4:'Robots',5:'Training',6:'Other'}
更改为
{'1':'electronics','2':'automobiles','3':'accessories','4':'Robots','5':'Training','6':'Other'}
df['product_id'] = [', '.join(d[y] for y in x) for x in df['product_id']]