pandas 对有关索引重复条目的警告 [英] Pandas pivot warning about repeated entries on index

查看:58
本文介绍了 pandas 对有关索引重复条目的警告的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

pivot方法的Pandas文档中,我们具有:

On Pandas documentation of the pivot method, we have:

Examples
--------
>>> df
    foo   bar  baz
0   one   A    1.
1   one   B    2.
2   one   C    3.
3   two   A    4.
4   two   B    5.
5   two   C    6.

>>> df.pivot('foo', 'bar', 'baz')
     A   B   C
one  1   2   3
two  4   5   6

我的DataFrame的结构如下:

   name   id     x
----------------------
0  john   1      0
1  john   2      0
2  mike   1      1
3  mike   2      0

我想要这样的东西:

      1    2   # (this is the id as columns)
----------------------
mike  0    0   # (and this is the 'x' as values)
john  1    0

但是当我运行pivot方法时,它是说:

But when I run the pivot method, it is saying:

*** ReshapeError: Index contains duplicate entries, cannot reshape

这没有任何意义,即使在示例中,在foo列上也有重复的条目.我正在使用name列作为数据透视表的索引,该轴是pivot方法调用的第一个参数.

Which doesn't makes sense, even in example there are repeated entries on the foo column. I'm using the name column as the index of the pivot, the first argument of the pivot method call.

推荐答案

对我有用吗?您可以张贴您正在使用的确切数据透视方法调用吗?

Works fine for me? Can you post the exact pivot method call you're using?

In [4]: df.pivot('name', 'id', 'x')
Out[4]: 
id    1  2
name      
john  0  0
mike  1  0

这篇关于 pandas 对有关索引重复条目的警告的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆