2017-10-05 5 views
1

でループを含んでいなかったufunc Iは、コードのこの部分を有する:パンダ - 「引く」署名マッチングタイプ

self.value=0.8 

for col in df.ix[:,'value1':'value3']: 
    df = df.iloc[abs(df[col] - self.value).argsort()] 

main()機能の一部として完全に機能します。復帰時に、それが印刷されます。私はモジュールの一部としてこの機能をインポート、と私は同じ0.8 self.valueを渡し、印刷てるにもかかわらず、とき

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 

は、しかし、私は次のエラーを取得する:

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
  1. pd.DataFrame.ixは廃止されました。あなたはそれをやめてください。
  2. 'value1':'value3'の使用は、あなたの列があなたが思った順序で配置されていない場合に期待しなかった列を含むことができるので危険です。

    list(df.loc[:, ['v1', 'v2', 'artist', 'v3', 'b']].loc[:, 'v1':'v3']) 
    
    ['v1', 'v2', 'artist', 'v3'] 
    

    df = pd.DataFrame(
        [['a', 'b', 1, 2, 3]], 
        columns='artist track v1 v2 v3'.split() 
    ) 
    
    list(df.loc[:, 'v1':'v3']) 
    
    ['v1', 'v2', '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') 
    

セットアップ
シャッフル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 
+0

ああ、素晴らしいです。実際に 'regex'が必要であるということを考えれば、質問を編集してcol名を変更する自由を取った。実際のcol名を熟考するために、あなたはとても親切であり、マッチを書き換えることができるだろうか... – outkast

+0

正規表現は必要。ちょうど3つの列の名前を使用してください。私は更新します。 – piRSquared