python - .loc() 的轴参数以解释轴上传递的切片器 = 1

标签 python pandas

documentation建议:

You can also specify the axis argument to .loc to interpret the passed slicers on a single axis.

但是我在尝试沿列索引切片时遇到错误。

import pandas as pd
import numpy as np

cols= [(yr,m) for yr in [2014,2015] for m in [7,8,9,10]]
df = pd.DataFrame(np.random.randint(1,100,(10,8)),index=tuple('ABCDEFGHIJ'))
df.columns =pd.MultiIndex.from_tuples(cols)

print df.head()

  2014             2015            
    7   8   9   10   7   8   9   10
A   68  51   6  48   24   3   4  85
B   79  75  68  62   19  40  63  45
C   60  15  32  32   37  95  56  38
D    4  54  81  50   13  64  65  13
E   78  21  84   1   83  18  39  57


#This does not work as expected
print df.loc(axis=1)[(2014,9):(2015,8)]
AssertionError: Start slice bound is non-scalar
#but an arbitrary transpose and changing axis works!
df = df.T
print df.loc(axis=0)[(2014,9):(2015,8)]

          A   B   C   D   E   F   G   H   I   J
2014 9    6  68  32  81  84  60  83  39  94  93
     10  48  62  32  50   1  84  18  14  92  33
2015 7   24  19  37  13  83  69  31  91  69  90
     8    3  40  95  64  18   8  32  93  16  25

所以我总是可以分配切片并重新转置。 这虽然感觉像是一个 hack,但 axis=1 设置应该有效。

df = df.loc(axis=0)[(2014,9):(2015,8)]

df = df.T


print df

  2014     2015    
    9   10   7   8 
A   64  98   99  87
B   43  36   22  84
C   32  78   86  66
D   67   8   34  73
E   83  54   96  33
F   18  83   36  71
G   13  25   76   8
H   69   4   99  84
I    3  52   50  62
J   67  60    9  49

最佳答案

这可能是一个错误。请在 github 上发布问题。选择事物的规范方法是完全指定所有轴。

In [6]: df.loc[:,(2014,9):(2015,8)]
Out[6]: 
  2014     2015    
    9   10   7   8 
A   26   2   44  69
B   41   7    5   1
C    8  27   23  22
D   54  72   81  93
E   18  23   54   7
F   11  81   37  83
G   60  38   59  29
H    3  95   89  96
I    6   9   77   9
J   90  92   10  32

关于python - .loc() 的轴参数以解释轴上传递的切片器 = 1,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31439314/

相关文章:

python - 在Python中的数据帧行中提取正则表达式匹配项,而不是组

python - 值错误: Can't convert non-rectangular Python sequence to Tensor

python - 将文件保存到 Amazon Web Service s3

python - KeyError : The tensor variable , 引用的张量不存在

python - 在 BeautifulSoup 中查找具有特定条件的 id

python - 如何计算每个项目的平均索引位置

python - 无法在 pandas 中执行具有灵活类型错误的 reduce

python - Django:每天在指定的用户本地时间运行一个进程

python - 如何用百分比制作 Pandas 交叉表?

python - Pandas 的速度 df.loc[x, 'column' ]