在具有分层索引的pandas数据框中使用iloc的问题 [英] Trouble with using iloc in pandas dataframe with hierarchical index

查看:78
本文介绍了在具有分层索引的pandas数据框中使用iloc的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

每当我尝试给具有层次结构索引的数据框上的iloc列表时,都会出现此ValueError.我不确定我是在做错什么还是这是一个错误.使用iloc和非分层索引时,我没有任何问题.这是使用Pandas 0.12.0.

I'm getting this ValueError whenever I try to give a list to iloc on a dataframe with a hierarchical index. I'm not sure if I'm doing something wrong or if this is a bug. I haven't had any issues using iloc the same way with a non-hierarchical index. This is using Pandas 0.12.0.

In [25]: df
Out[25]: 
            D         E         F
a x -1.050681 -0.084306 -1.635852 
  y  1.544577  1.594976 -0.084866
b x  0.462529 -1.873250  1.252685
  y -0.468074  0.673112 -0.900547
c x  0.901710 -0.432554  0.260157
  y  0.101522 -0.550223  1.389497

In [26]: df.iloc[[1,3]]
..... snip .....
ValueError: Buffer dtype mismatch, expected 'Python object' but got 'long'

In [27]: df.iloc[range(2)]
...... snip .....
ValueError: Buffer dtype mismatch, expected 'Python object' but got 'long'

推荐答案

这是一个错误并已在master(0.13)中修复,一个临时的解决方法是使用ix(!):

This was a bug and has been fixed in master (0.13), a temporary workaround is to use ix (!):

In [11]: df1.ix[[1, 3]]
Out[11]: 
            D         E         F
a y  1.544577  1.594976 -0.084866
b y -0.468074  0.673112 -0.900547

在主版本中,为0.13:

In master, 0.13:

In [12]: df1.iloc[[1, 3]]
Out[12]: 
            D         E         F
a y  1.544577  1.594976 -0.084866
b y -0.468074  0.673112 -0.900547

这篇关于在具有分层索引的pandas数据框中使用iloc的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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