简体   繁体   中英

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

尝试这个,

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

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