Pandas 熊猫-乌芬奇';减去';未包含签名类型匹配的循环

Pandas 熊猫-乌芬奇';减去';未包含签名类型匹配的循环,pandas,Pandas,我有一段代码: self.value=0.8 for col in df.ix[:,'value1':'value3']: df = df.iloc[abs(df[col] - self.value).argsort()] 作为main()函数的一部分,它可以完美地工作。返回时,它会打印: artist track pos neg neu 4 Sufjan Stevens C

我有一段代码:

self.value=0.8

for col in df.ix[:,'value1':'value3']:
    df = df.iloc[abs(df[col] - self.value).argsort()]
作为
main()
函数的一部分,它可以完美地工作。返回时,它会打印:

    artist          track                      pos     neg          neu
4   Sufjan Stevens       Casimir Pulaski Day   0.09    0.91          0.0
9   Sufjan Stevens            The Only Thing   0.09    0.91          0.0
5        Radiohead        Desert Island Disk   0.08    0.92          0.0
0   Sufjan Stevens  Should Have Known Better   0.07    0.93          0.0
1   Sufjan Stevens      To Be Alone With You   0.05    0.95          0.0
8        Radiohead               Daydreaming   0.05    0.95          0.0
3   Sufjan Stevens        Death with Dignity   0.03    0.97          0.0
11   Elliott Smith          Between the Bars   0.03    0.97          0.0
2     Jeff Buckley                Hallelujah   0.39    0.61          0.0
6        Radiohead                     Codex   0.00    1.00          0.0
7       Aphex Twin                Avril 14th   0.00    1.00          0.0
10       Radiohead       You And Whose Army?   0.00    1.00          0.0
但是,当我将此函数作为模块的一部分导入时,即使我传递并打印相同的
0.8 self.value
,我仍会收到以下错误:

    df = df.iloc[(df[col] - self.flavor).argsort()]
  File "/Users/me/anaconda/lib/python2.7/site-packages/pandas/core/ops.py", line 721, in wrapper
    result = wrap_results(safe_na_op(lvalues, rvalues))
  File "/Users/me/anaconda/lib/python2.7/site-packages/pandas/core/ops.py", line 682, in safe_na_op
    return na_op(lvalues, rvalues)
  File "/Users/me/anaconda/lib/python2.7/site-packages/pandas/core/ops.py", line 668, in na_op
    result[mask] = op(x[mask], y)
TypeError: ufunc 'subtract' did not contain a loop with signature matching types dtype('S32') dtype('S32') dtype('S32')
为什么会这样?发生了什么事

  • pd.DataFrame.ix
    is已被弃用。你应该停止使用它
  • 您使用
    'value1':'value3'
    是危险的,因为如果您的列未按您认为的顺序放置,它可能会包含您意想不到的列

    df = pd.DataFrame(
        [['a', 'b', 1, 2, 3]],
        columns='artist track v1 v2 v3'.split()
    )
    
    list(df.loc[:, 'v1':'v3'])
    
    ['v1', 'v2', 'v3']
    
    但是重新排列列和

    list(df.loc[:, ['v1', 'v2', 'artist', 'v3', 'b']].loc[:, 'v1':'v3'])
    
    ['v1', 'v2', 'artist', 'v3']
    
    列表中有
    “艺术家”
    。列
    'artist'
    是字符串类型,不能从整数或浮点中减去或被减去

    df['artist'] - df['v1']
    
    > TypeError: ufunc 'subtract' did not contain a loop with signature matching types dtype('<U21') dtype('<U21') dtype('<U21')
    

    解决方案
    使用
    np.lexsort

    value = 0.8
    
    v = df[['pos', 'neg', 'neu']].values
    
    df.iloc[np.lexsort(np.abs(v - value).T)]
    
                artist                     track     pos     neg     neu
    4   Sufjan Stevens       Casimir Pulaski Day    0.09    0.91     0.0
    9   Sufjan Stevens            The Only Thing    0.09    0.91     0.0
    5        Radiohead        Desert Island Disk    0.08    0.92     0.0
    0   Sufjan Stevens  Should Have Known Better    0.07    0.93     0.0
    8        Radiohead               Daydreaming    0.05    0.95     0.0
    1   Sufjan Stevens      To Be Alone With You    0.05    0.95     0.0
    11   Elliott Smith          Between the Bars    0.03    0.97     0.0
    3   Sufjan Stevens        Death with Dignity    0.03    0.97     0.0
    2     Jeff Buckley                Hallelujah    0.39    0.61     0.0
    7       Aphex Twin                Avril 14th    0.00    1.00     0.0
    6        Radiohead                     Codex    0.00    1.00     0.0
    10       Radiohead       You And Whose Army?    0.00    1.00     0.0
    

    哦,太好了。但是,考虑到现在需要
    regex
    ,我冒昧地编辑了这个问题并更改了列名称……您能不能如此友好地重写匹配项以考虑真正的列名称?regex不是必需的。只需使用这三列名称。我会更新。
    value = 0.8
    
    v = df[['pos', 'neg', 'neu']].values
    
    df.iloc[np.lexsort(np.abs(v - value).T)]
    
                artist                     track     pos     neg     neu
    4   Sufjan Stevens       Casimir Pulaski Day    0.09    0.91     0.0
    9   Sufjan Stevens            The Only Thing    0.09    0.91     0.0
    5        Radiohead        Desert Island Disk    0.08    0.92     0.0
    0   Sufjan Stevens  Should Have Known Better    0.07    0.93     0.0
    8        Radiohead               Daydreaming    0.05    0.95     0.0
    1   Sufjan Stevens      To Be Alone With You    0.05    0.95     0.0
    11   Elliott Smith          Between the Bars    0.03    0.97     0.0
    3   Sufjan Stevens        Death with Dignity    0.03    0.97     0.0
    2     Jeff Buckley                Hallelujah    0.39    0.61     0.0
    7       Aphex Twin                Avril 14th    0.00    1.00     0.0
    6        Radiohead                     Codex    0.00    1.00     0.0
    10       Radiohead       You And Whose Army?    0.00    1.00     0.0