熊猫函数以基于单列创建多个列
我有一个看起来像这样的数据集
A | B | C |
---|---|---|
A1 | 1 | 1.3 |
A1 | 2 | 1.4 |
A1 | 3 | 1.3 |
A2 | 1 | 1.6 |
A2 | A2 2 | 1.7 |
A2 | 3 | 1.5 |
A1 | 1 A1 | 1 A1 1 |
A1 | 2 | 2.7 |
A1 | 3 | 2.4 |
A2 | 1 | 1.6 |
A2 1 1.6 A2 | 2 | 1.7 |
A2 A2 | 3 | 1.2 |
所需的输出
A | 1 | 2 | 3 |
---|---|---|---|
A1 | 1.3 | 1.4 | 1.3 |
A1 | 1 | 2.7 | 2.7 |
A2 | 1.7 | 1.6 1.6 | 1.5 |
A2 | 1.6 | 1.7 | 1.2 |
我尝试了一个枢轴表,但它给出了聚合,没有聚合枢轴,它显示出重复的索引错误,因为重复的索引。处理此问题的最佳方法是什么?
I have a dataset which looks like this
A | B | C |
---|---|---|
A1 | 1 | 1.3 |
A1 | 2 | 1.4 |
A1 | 3 | 1.3 |
A2 | 1 | 1.7 |
A2 | 2 | 1.6 |
A2 | 3 | 1.5 |
A1 | 1 | 1 |
A1 | 2 | 2.7 |
A1 | 3 | 2.4 |
A2 | 1 | 1.6 |
A2 | 2 | 1.7 |
A2 | 3 | 1.2 |
Desired output :
A | 1 | 2 | 3 |
---|---|---|---|
A1 | 1.3 | 1.4 | 1.3 |
A1 | 1 | 2.7 | 2.4 |
A2 | 1.7 | 1.6 | 1.5 |
A2 | 1.6 | 1.7 | 1.2 |
I have tried a pivot table but it's giving aggregation and without aggregation pivot it is showing a duplicate index error because of the duplicate index. What is the best way to handle this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
方式进行迭代:
组合都具有相同数量的元素)与以下
您可以使用数量的元素数量(假设所有 简单的枢轴应该做:
You can iterate over using number of elements (assuming all combos have same number of elements) as follows:
Old answer:
Seeing your sample dataframe, a simple pivot should do:
尝试:
打印:
Try:
Prints:
一种选项是创建一个额外的列,在旋转之前,将其引入您的数据唯一性:
One option is to create an extra column that introduces uniqueness to your data, before pivoting: