2016-10-25 24 views
0

私は次のコードを持つ2つの列の値を比較しようとしている:パンダに2つの列を比較

if df['predicted_spread'] > df['vegas_spread']: 
total_bet += 1 

それは次のエラーを返して:

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

任意のアイデアどのように私ができますこれを修正しますか?

+0

これを見てください:http://stackoverflow.com/questions/10062954/valueerror-the-truth-value-of-an-array-with-more-than-one-element-is-ambiguous – wwl

+0

あなた最初にあなたのロジックをチェックして、何を達成したいのですか? 'df ['predicted_spread']> df ['vegas_spread']'は論理値を期待している間に論理系列を返します。 – Psidom

答えて

3

最初に、df['predicted_spread'] > df['vegas_spread']の結果はブール値のブール値で構成されています(例:[True, True, False, ...])ので、エラーメッセージThe truth value of a Series is ambiguousが表示されます。

あなたは何をすべきですか?それはあなたのアプリケーションに依存します。

(1)あなたの意図が

if (df['predicted_spread'] > df['vegas_spread']).all() is True: 
    total_bet += 1 
そして、その後、

total_bet = sum(df['predicted_spread'] > df['vegas_spread']) 

(2)あなたの意図はdf['predicted_spead'] > df['vegas_spread']のすべてcomparationがtrueの場合total_betを増やすことであるならば、True条件の数を数えることであるならば

(3)total_betもベクトルであり、あなたの意図は、各comparationを記録することであれば、その後、

total_bet = total_bet + (df['predicted_spread'] > df['vegas_spread']) 

私はオプション(1)が必要なものだと思います。ありがとう。