我在熊貓DF中有一些數據,並希望根據一些布爾條件來分離它的特定部分。下面兩行工作,我希望他們能夠:使用布爾語句來處理熊貓系列
df['test'] = df[df.N == 30].my_variable
df['test2'] = df[df.Y >0.4].my_variable
由於不是那個熟悉的大熊貓可是我再試着寫:
df['test'] = df[df.N == 30 & df.Y >0.4].my_variable
但是,這將引發以下錯誤:
TypeError: ufunc 'bitwise_and' not supported for the input types, and the inputs could not be safely coerced to any supported types according to the casting rule ''safe''
任何人都可以提出正確的(或更好的)方式來實現我想要做的事情嗎?
不熟悉熊貓,但不應該使用'和'而不是'&'做一個邏輯「AND」? – senshin
您是否曾嘗試在各個過濾器周圍放置括號?例如:'df [(df ['col1']> = 1)&(df ['col1'] <= 1)]' –
哦,這也可能是一個運算符優先級問題,因爲'&比'=='和'>'做得更好,所以圍繞你的條件的括號也是一件好事。 – senshin