2017-02-03 10 views
1

私のデータフレームにはすでに2つのブール列があります。lowhighです。次に、in_range列を定義します。これは、行内のlow列とhigh列の両方がfalseである状況を反映しています。ここに私が試したものです:他の2つのブール列に依存するpandasブール列を作成する

df['in_range'] = not (df.low or df.high) 

これは、エラーが発生します。

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

私は、行単位で上でこれをやってみたいです。

ValueError: can only convert an array of size 1 to a Python scalar

は私のコードの何が問題になっており、既存のブール列に基づいて新しいブール型の列を生成するための適切な方法は何です:私は、このエラーが発生.bool()を追加しようとしましたか?

答えて

0

ビット単位の構文を使用します。

~(df.low | df.high) 

documentationを参照してください:

シリーズは正確にnumpyのndarrayのように働くのインデックスにブールベクトルを使用して

Another common operation is the use of boolean vectors to filter the data. The operators are: | for or, & for and, and ~ for not. These must be grouped by using parentheses.

関連する問題