我有一张有客户ID和电子邮件的桌子。有些用户有多封电子邮件。该表如下所示:
| Customer | Email |
| ----------| -------------- |
| 1 | jdoe@mail.com |
| 2 | jane1@mail.com |
| 3 | adam@mail.com |
| 1 | john_d@mail.com|
我想做的是重新安排表,使每个客户ID只有一行,并将辅助电子邮件添加为附加列。大概是这样的:
| Customer | Email1 |Email2 |
| ----------| -------------- |---------------|
| 1 | jdoe@mail.com |john_d@mail.com
| 2 | jane1@mail.com | |
| 3 | adam@mail.com | |
用熊猫做这个最好的方法是什么?我尝试过使用df.pivot,但这似乎对我不起作用
您可以使用
cumcount
创建多索引。然后使用unstack
重新格式化数据,并通过add_prefix
添加更改列名称:你会得到你想要的
您可以使用^{} +^{} +^{}
输出:
相关问题 更多 >
编程相关推荐