2017-07-12 17 views
0

この質問は以前私の質問に強く関連しています。 here
もう一度お詫び申し上げます。列の値を分類する際のPythonのパフォーマンスの問題

以下のコードは実行されていますが、正しい結果を返していますが、やはり遅い(80K行では4分)です。私は具体的な値のためにパンダからSeriesクラスを使うのに問題があります。誰かが代わりにそれらの列を分類する方法をお勧めしますか?

はドキュメンタリーに関連する情報を見つけることができませんでした:
https://pandas.pydata.org/pandas-docs/stable/generated/pandas.Series.html

実行コード:

# p_test_SOLL_test_D10 

for x in range (0,len(tableContent[6])): 
    var = tableContent[6].loc[x, ('p_test_LAENGE')] 
    if float(tableContent[6].loc[x, ('p_test_LAENGE')])>=100.0: 
     tableContent[6].loc[x, ('p_test_LAENGE')]='yes' 
    elif (float(tableContent[6].loc[x, ('p_test_LAENGE')]) <30.0 and float(tableContent[6].loc[x, ('p_test_LAENGE')]) >= 10): 
     tableContent[6].loc[x, ('p_test_LAENGE')]='yes2' 
    elif (float(tableContent[6].loc[x, ('p_test_LAENGE')]) <10.0 and float(tableContent[6].loc[x, ('p_test_LAENGE')]) >= 5): 
     tableContent[6].loc[x, ('p_test_LAENGE')]='yes3' 
    else: 
     tableContent[6].loc[x, ('p_test_LAENGE')]='no' 

print (tableContent[6]['p_test_LAENGE']) 

シリーズ試してください。

if tableContent[6]['p_test_LAENGE'].astype(float) >=100.0: 
    tableContent[6]['p_test_LAENGE']='yes' 
elif (tableContent[6]['p_test_LAENGE'].astype(float) <30.0 and tableContent[6]['p_test_LAENGE'].astype(float) >= 10): 
    tableContent[6]['p_test_LAENGE']='yes1' 
elif (tableContent[6]['p_test_LAENGE'].astype(float) <10.0 and tableContent[6]['p_test_LAENGE'].astype(float) >= 5): 
    tableContent[6]['p_test_LAENGE']='yes2' 
else: 
    tableContent[6]['p_test_LAENGE']='no' 


print (tableContent[6]['p_test_LAENGE']) 
+0

(おそらく今この質問を見て)パンダと方法より経験豊富な人がいます。だから、私は標準的なアプローチをあなたにも与えようとはしません。しかし、以前の質問のアドバイスは、「ベクトル化」と「ループを取り除く」と言います。 'for'ループを使用しているのであれば、それらはおおよそPythonの時間(計算上)で実行され、' numpy'や 'pandas'を気にしないでバニラのpythonに行くこともできます。それは思考の変化を必要とし、いくつかのチュートリアルは、先に進む前にここでうまくいくかもしれません。 – roganjosh

答えて

1

私はあなたのdfはあなたが必要なので、テストする必要はありませんが次のコードを変更します。 はdfの最大値未満10e7

bin = [10e-7,5,10,30,100,10e7] 
label = ['no','yes2','yes1','no','yes'] 
df['p_test_LAENGE_class'] = pd.cut(df['p_test_LAENGE'], bins=bin, labels=label) 

希望ですこれはあなたを助けるながらdfの分を超える10e-7であると仮定し

+0

ありがとう!カット方法は私が探していたものでした! – Bene

関連する問題