Python np.r_u不适用于2个范围或多个切片

Python np.r_u不适用于2个范围或多个切片,python,numpy,Python,Numpy,我的数据框形状(199,7)。我想选择列0:4和-2:(最后两列)。我想获得所选列的行,这些列包含[final_data['unemployed']=='yes']。我的专栏是 [‘恐慌症发作’、‘强迫性行为’、‘抑郁’、‘焦虑’、‘疲劳’、‘失业’、‘集群’] My first trial返回错误ValueError:特殊指令必须是第一个条目 我的第二次尝试不知何故它无法工作(可能是因为.iloc,如果我错了,请纠正我) 他们为什么不工作?我应该怎么做?这是一种产生错误消息的表达式。我在你的样

我的数据框形状(199,7)。我想选择列0:4和-2:(最后两列)。我想获得所选列的行,这些列包含
[final_data['unemployed']=='yes']
。我的专栏是

[‘恐慌症发作’、‘强迫性行为’、‘抑郁’、‘焦虑’、‘疲劳’、‘失业’、‘集群’]

  • My first trial返回错误
    ValueError:特殊指令必须是第一个条目
  • 我的第二次尝试不知何故它无法工作(可能是因为.iloc,如果我错了,请纠正我)

  • 他们为什么不工作?我应该怎么做?

    这是一种产生错误消息的表达式。我在你的样品中没有看到:

    In [158]: np.r_[1:3, '-1']                                                                             
    ---------------------------------------------------------------------------
    ValueError                                Traceback (most recent call last)
    <ipython-input-158-0b702ddf8054> in <module>
    ----> 1 np.r_[1:3, '-1']
    
    /usr/local/lib/python3.6/dist-packages/numpy/lib/index_tricks.py in __getitem__(self, key)
        358             elif isinstance(item, str):
        359                 if k != 0:
    --> 360                     raise ValueError("special directives must be the "
        361                             "first entry.")
        362                 if item in ('r', 'c'):
    
    ValueError: special directives must be the first entry.
    

    我不太确定你想要什么第二档。请记住,对于
    r\u
    ,负范围很棘手,
    r\u
    不知道
    df.列的大小

    编辑
    [154]
    之所以有效,是因为我的示例数据帧具有数字列标题。将其更改为字符串:

    In [173]: df = pd.DataFrame(np.arange(21).reshape(3,7),columns=list('abcdefg'))                        
    In [174]: df                                                                                           
    Out[174]: 
        a   b   c   d   e   f   g
    0   0   1   2   3   4   5   6
    1   7   8   9  10  11  12  13
    2  14  15  16  17  18  19  20
    
    In [176]: df[np.r_[df.columns[0:4],df.columns[2]]]                                                     
    ....
    ValueError: special directives must be the first entry.
    
    引起错误的是
    r\uu

    In [177]: np.r_[df.columns[0:4],df.columns[2]]                                                         
    ---------------------------------------------------------------------------
    ValueError                                Traceback (most recent call last)
    <ipython-input-177-c0b1d20ac1a7> in <module>
    ----> 1 np.r_[df.columns[0:4],df.columns[2]]
    
    /usr/local/lib/python3.6/dist-packages/numpy/lib/index_tricks.py in __getitem__(self, key)
        358             elif isinstance(item, str):
        359                 if k != 0:
    --> 360                     raise ValueError("special directives must be the "
        361                             "first entry.")
        362                 if item in ('r', 'c'):
    
    ValueError: special directives must be the first entry.
    
    看起来最简单的解决方法是使用
    hstack
    (或者只是“连接
    ”)而不是
    r\ucode>。此列表不需要对切片进行特殊处理:

    In [182]: np.hstack((df.columns[0:4],df.columns[2]))                                                   
    Out[182]: array(['a', 'b', 'c', 'd', 'c'], dtype=object)
    
    In [183]: df[np.hstack((df.columns[0:4],df.columns[2]))]                                               
    Out[183]: 
        a   b   c   d   c
    0   0   1   2   3   2
    1   7   8   9  10   9
    2  14  15  16  17  16
    

    对不起,我的意思是
    final_data.iloc[:,np.r\u[0:4,-2:]
    所以第二个范围是2列。我试过了,但没有成功,可能是因为正如你所说的负范围很棘手,r_u不知道df的大小。列
    自己测试
    r_u
    。每个片段都用一个
    arange(start,stop,step)
    表达式展开。
    r
    需要用一个简单的
    hstack
    替换。
    In [150]: np.r_[0:4,-2:-1]                                                                                               
    Out[150]: array([ 0,  1,  2,  3, -2])
    In [156]: df.iloc[:,np.r_[0:4,-2:-1]] 
         ...:  
         ...:                                                                                              
    Out[156]: 
        0   1   2   3   5
    0   0   1   2   3   5
    1   7   8   9  10  12
    2  14  15  16  17  19
    
    In [173]: df = pd.DataFrame(np.arange(21).reshape(3,7),columns=list('abcdefg'))                        
    In [174]: df                                                                                           
    Out[174]: 
        a   b   c   d   e   f   g
    0   0   1   2   3   4   5   6
    1   7   8   9  10  11  12  13
    2  14  15  16  17  18  19  20
    
    In [176]: df[np.r_[df.columns[0:4],df.columns[2]]]                                                     
    ....
    ValueError: special directives must be the first entry.
    
    In [177]: np.r_[df.columns[0:4],df.columns[2]]                                                         
    ---------------------------------------------------------------------------
    ValueError                                Traceback (most recent call last)
    <ipython-input-177-c0b1d20ac1a7> in <module>
    ----> 1 np.r_[df.columns[0:4],df.columns[2]]
    
    /usr/local/lib/python3.6/dist-packages/numpy/lib/index_tricks.py in __getitem__(self, key)
        358             elif isinstance(item, str):
        359                 if k != 0:
    --> 360                     raise ValueError("special directives must be the "
        361                             "first entry.")
        362                 if item in ('r', 'c'):
    
    ValueError: special directives must be the first entry.
    
    In [178]: df.columns[0:4]                                                                              
    Out[178]: Index(['a', 'b', 'c', 'd'], dtype='object')
    
    In [182]: np.hstack((df.columns[0:4],df.columns[2]))                                                   
    Out[182]: array(['a', 'b', 'c', 'd', 'c'], dtype=object)
    
    In [183]: df[np.hstack((df.columns[0:4],df.columns[2]))]                                               
    Out[183]: 
        a   b   c   d   c
    0   0   1   2   3   2
    1   7   8   9  10   9
    2  14  15  16  17  16