python - 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))
推荐阅读
- c++ - 使用 arm-none-eabi 进行 Clang 交叉编译
- imagemagick - ImageMagick - 从 Photoshop 文件中删除组
- vue.js - Quasar Framework (Vue):可折叠和鼠标离开
- amazon-web-services - Route 53 + ELB = 不起作用,但似乎配置正确
- docker - 用./configure配置了一个下载的包,如何从centos中彻底删除
- node.js - Microsoft bot 框架 IE11 嵌入问题
- excel - 是否有一个 VBA 字符串来选择曾经有一个空白公式的复制单元格?
- html - 如何使块从新位置继续
- jquery - 差异不是函数
- php - 如何在api php中处理mysql超时问题