熊猫 - ufunc'减去'没有包含签名匹配类型的循环

时间:2017-10-05 03:58:16

标签: 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       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')

为什么会这样?发生了什么事?

1 个答案:

答案 0 :(得分:1)

  1. pd.DataFrame.ix已被弃用。你应该停止使用它。
  2. 您使用'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'。列'artist'的类型为字符串,不能从整数或浮点数中减去。

    df['artist'] - df['v1']
    
    > TypeError: ufunc 'subtract' did not contain a loop with signature matching types dtype('<U21') dtype('<U21') dtype('<U21')
    
  3. <强>设置
    随机播放df

    df = df.sample(frac=1)
    df
    
                artist                     track     pos     neg     neu
    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
    5        Radiohead        Desert Island Disk    0.08    0.92     0.0
    11   Elliott Smith          Between the Bars    0.03    0.97     0.0
    7       Aphex Twin                Avril 14th    0.00    1.00     0.0
    2     Jeff Buckley                Hallelujah    0.39    0.61     0.0
    4   Sufjan Stevens       Casimir Pulaski Day    0.09    0.91     0.0
    9   Sufjan Stevens            The Only Thing    0.09    0.91     0.0
    3   Sufjan Stevens        Death with Dignity    0.03    0.97     0.0
    6        Radiohead                     Codex    0.00    1.00     0.0
    10       Radiohead       You And Whose Army?    0.00    1.00     0.0
    

    <强>解决方案
    使用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