2016-06-15 3 views
1

私は以下のコードを持っています。私がしたいのは、.locを使ってアクセスしたセルの値を見て、If文を使って3つの条件をテストすることです。私は戻って取得パンダの.locに対する応答を評価する際の問題

メッセージは、私はパンダはこれがなぜ起こるかで一つだけのアイテムがある場合でもシリーズを返すと思い

ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().

です。しかし、私は任意の()を使用してみたときに私はメッセージを得た

AttributeError: 'str' object has no attribute 'any'

私は立ち往生しています。どんな助けも非常に高く評価されるでしょう。

for my_index, row in PPI_data.iterrows(): 
     if PPI_data.loc[my_index,"Incident counter"]<3 and PPI_data.loc[my_index,"Risk Level"]=="Standard" and PPI_data.loc[my_index,"Crime number"]=='Not crime': 
      PPI_data.set_value(my_index, 'Eligability to visit', "Eligable") 
     else: 
      PPI_data.set_value(my_index, 'Eligability to visit', "Not eligable") 

答えて

1

私はあなたがiterrowsとしてmaskではなくnumpy.whereを使用することができると思う:サンプル

mask = (PPI_data["Incident counter"]<3) & 
     (PPI_data["Risk Level"]=="Standard") & 
     (PPI_data["Crime number"]=='Not crime') 

PPI_data['Eligability to visit'] = np.where(mask,'Eligable', 'Not eligable') 

:このため

import pandas as pd 

PPI_data = pd.DataFrame({'Incident counter':[5,2,1], 
        'Risk Level':["Standard","Standard","Standard"], 
        'Crime number':['Not crime',"Not crime","Crime"]}) 

print (PPI_data) 
    Crime number Incident counter Risk Level 
0 Not crime     5 Standard 
1 Not crime     2 Standard 
2  Crime     1 Standard 

mask = (PPI_data["Incident counter"] < 3) & 
     (PPI_data["Risk Level"]=="Standard") & 
     (PPI_data["Crime number"]=='Not crime') 

print (mask) 
0 False 
1  True 
2 False 
dtype: bool 

PPI_data['Eligability to visit'] = np.where(mask,'Eligable', 'Not eligable') 
print (PPI_data) 
    Crime number Incident counter Risk Level Eligability to visit 
0 Not crime     5 Standard   Not eligable 
1 Not crime     2 Standard    Eligable 
2  Crime     1 Standard   Not eligable 
+0

おかげで私はTypeError例外を得た:unorderableタイプ:STR()

+0

申し訳ありませんが、コードは 'print(PPI_data [" Incident counter "] <3)'で失敗しますか? – jezrael

+0

私は何をしたのか分かります。あなたのコードを間違ってコピーしました。あなたの提案はうまくいきます。私はとても嬉しいです - 多くの感謝 –

関連する問題