您的位置: 网站首页> 大家问> 当前文章

Index contains duplicate entries, cannot reshape完美解决

老董2022-04-30156围观,126赞

  Index contains duplicate entries, cannot reshape,这个错误是做数据变形(比如多行变多列、多列变多行)时可能会出现的错误。数据变形常见的函数有pivot、unstack的时候遇见过,我们来感受下这个错误。

  1、先看1个多行变多列正常的案例

# -*- coding:UTF-8 -*-
import pandas as pd   

df = pd.read_excel('11.xlsx')
print(df)
print('------------')
df_res = df.pivot(index='name',columns='week')
print(df_res)
   name  week money
0  wang     1  100$
1  wang     2  110$
2  dong     1  120$
3  dong     2  150$
------------
     money      
week     1     2
name            
dong  120$  150$
wang  100$  110$

  2、多行变多列引起报错的案例

  index参数所指的列和column参数所值的列,这两列的组合有重复的行数据,那么就会报错Index contains duplicate entries, cannot reshape,如下name和week有2行是重复的。

# -*- coding:UTF-8 -*-
import pandas as pd   

df = pd.read_excel('11.xlsx')
print(df)
print('------------')
df_res = df.pivot(index='name',columns='week')
print(df_res)
   name  week money
0  wang     1  100$
1  wang     2  110$
2  wang     2  220$
3  dong     1  120$
4  dong     2  150$
------------
    raise ValueError("Index contains duplicate entries, cannot reshape")
ValueError: Index contains duplicate entries, cannot reshape

  3、再看1个unstack函数的案例

  先看1个正确的变换,再看1个错误的变换,注意看之所以报错是因为有重复的行数据。

# -*- coding:UTF-8 -*-
df = pd.read_excel('11.xlsx',index_col=[0,1])
print(df)
print('------------')
print(df.unstack(level=-1))
          value
baidu pc    100
      mo    200
sogou pc   1000
      mo   2000
------------
      value      
         mo    pc
baidu   200   100
sogou  2000  1000

# -*- coding:UTF-8 -*-
df = pd.read_excel('11.xlsx',index_col=[0,1])
print(df)
print('------------')
print(df.unstack(level=-1))
          value
baidu pc    100
      mo    200
      pc    100
sogou pc   1000
      mo   2000
------------
 raise ValueError("Index contains duplicate entries, cannot reshape")
ValueError: Index contains duplicate entries, cannot reshape

  不过,我用stack()函数的时候还没遇见过这个情况!

  通过以上例子,想必大家已经知道了Index contains duplicate entries, cannot reshape的缘由,学习更多pandas知识欢迎大家关注pandas实战教程

很赞哦!

python编程网提示:转载请注明来源www.python66.com。
有宝贵意见可添加站长微信(底部),获取技术资料请到公众号(底部)。同行交流请加群 python学习会

文章评论

    Index contains duplicate entries, cannot reshape完美解决文章写得不错,值得赞赏

站点信息

  • 网站程序:Laravel
  • 客服微信:a772483200