首页 > 解决方案 > I am trying to pivot a dataset with pandas

问题描述

This is the sample of the dataset

https://i.stack.imgur.com/YtESW.png

This is the result i am trying to achieve

https://i.stack.imgur.com/GnHpS.png

Here is my code:

df.pivot( values = ['Image Name'],index=['sku','Name of product']).reset_index()

Here is the dataset

sku,Name of product,Image Name

155280,Catalinaresin planter in white H 100cm,Catalinaresin planter in white H 100cm.jpg
155280,Catalinaresin planter in white H 100cm,Catalinaresin planter in white H 100cm2.jpg
155280,Catalinaresin planter in white H 100cm,Catalinaresin planter in white H 100cm3.jpg
155280,Catalinaresin planter in white H 100cm,Catalinaresin planter in white H 100cm4.jpg

标签: pythonpandaspivot

解决方案


尝试这个,

 pd.pivot_table(data=df,index='sku',columns='Image Name',values='Name of product',aggfunc=lambda x: ' '.join(str(v) for v in x))

推荐阅读