2017-10-05 8 views
0

私はこのデータフレームを持っている:パンダ - 最大値によってデータフレームの並べ替え行単位

私はそうのよう input_value = 0.8

への近さに応じて仕分けしてい

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 1.00 0.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 1.00 0.00  0.0 
10  Radiohead  You And Whose Army? 0.00 1.00  0.0 

v = df[['pos', 'neg', 'neu']].values 
    df.iloc[np.lexsort(np.abs(v - input_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 
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 1.00 0.00   0.0 
7  Aphex Twin    Avril 14th 1.00 0.00   0.0 
10  Radiohead  You And Whose Army? 0.00 1.00   0.0 

私は、条件が満たされない場合if input_label = 'neg'

次いでneg値が最高値row-wiseでなければならない、

はそれに応じて行を排除するという条件を挿入する

を希望input_label = 'neg'

を与え次のようになります。

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 
10  Radiohead  You And Whose Army? 0.00 1.00   0.0 

どうすればいいですか?

+0

私は従うことが本当にできないんだけどきみの 質問。行インデックス6と7を削除したようです。これらの行の両方のネガティブ値が0であることがわかりますが、あなたが目指しているものとどのように関係しているかわかりません。ブーリアンマスクがあなたが探しているようなものです。 df [df ['neg'] == input_label] –

+0

私はデータフレーム内で正しくラベル付けされたアイテムだけを保持することを目指しています。行の 'pos'値が最も高い場合、これらの項目はここに属しません。私は 'neg'トラックが必要です。私の目的は、1)違いによってデータフレームを並べ替え、2)間違ったエントリを削除することです。 –

+0

temp = df [df ['neg'] - df ['pos']> 0]は、negがposより大きいレコードのサブセットを提供します。それはあなたが達成しようとしていることですか? – PhilC

答えて

0
v = df.iloc[:, -3:] 
df = df.iloc[np.lexsort(np.abs(v - input_value).T)] 

あなたは物事を単純化する必要があり、ここでdf.queryを使用することができます。 np.argmax

result = df.query('neg > pos and neg > neu'); result 
      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 
10  Radiohead  You And Whose Army? 0.00 1.00 0.0 

代替ソリューション:

mask = np.argmax(df.iloc[:, -3:].values, 1) == 1 

mask 
array([ True, True, True, True, True, True, True, False, True, 
     True, True, False], dtype=bool) 

result = df[mask]; result  
      artist      track pos neg neu 
11 Elliott Smith   Between the Bars 0.03 0.97 0.0 
2  Jeff Buckley    Hallelujah 0.39 0.61 0.0 
0 Sufjan Stevens Should Have Known Better 0.07 0.93 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 
5  Radiohead  Desert Island Disk 0.08 0.92 0.0 
1 Sufjan Stevens  To Be Alone With You 0.05 0.95 0.0 
3 Sufjan Stevens  Death with Dignity 0.03 0.97 0.0 
10  Radiohead  You And Whose Army? 0.00 1.00 0.0 
8  Radiohead    Daydreaming 0.05 0.95 0.0 

あなたはsort_indexを使用して、インデックスにあなたのdfを並べ替えることができます。

result.sort_index() 
      artist      track pos neg neu 
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 
2  Jeff Buckley    Hallelujah 0.39 0.61 0.0 
3 Sufjan Stevens  Death with Dignity 0.03 0.97 0.0 
4 Sufjan Stevens  Casimir Pulaski Day 0.09 0.91 0.0 
5  Radiohead  Desert Island Disk 0.08 0.92 0.0 
8  Radiohead    Daydreaming 0.05 0.95 0.0 
9 Sufjan Stevens   The Only Thing 0.09 0.91 0.0 
10  Radiohead  You And Whose Army? 0.00 1.00 0.0 
11 Elliott Smith   Between the Bars 0.03 0.97 0.0 
+0

を受け入れるでしょう。文字列の代わりに 'input_label'のような変数を使う必要があるので、Argmaxを使った解決策が最適かもしれません。私は変数の条件を満たす必要があります。 –

+0

@data_garden確かに。 –

関連する問題