我收到錯誤時,我對數據框中的單個元素進行比較,但我不明白爲什麼。熊貓布爾comparisson對數據框
我有時間序列數據的數據幀DF爲一些客戶,一些空值在其中:
df.head()
8143511 8145987 8145997 8146001 8146235 8147611 \
2012-07-01 00:00:00 NaN NaN NaN NaN NaN NaN
2012-07-01 00:30:00 0.089 NaN 0.281 0.126 0.190 0.500
2012-07-01 01:00:00 0.090 NaN 0.323 0.141 0.135 0.453
2012-07-01 01:30:00 0.061 NaN 0.278 0.097 0.093 0.424
2012-07-01 02:00:00 0.052 NaN 0.278 0.158 0.170 0.462
在我的劇本,行 if pd.isnull(df[[customer_ID]].loc[ts]):
產生一個錯誤:
ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().
但是,如果我在腳本行上放置了一個斷點,並且腳本停止時,我將它輸入到控制檯中:
pd.isnull(df[[customer_ID]].loc[ts])
輸出爲:
8143511 True
Name: 2012-07-01 00:00:00, dtype: bool
如果我允許腳本從該點繼續,則立刻產生錯誤。
如果可以計算布爾表達式並且其值爲True
,爲什麼它會在if表達式中生成一個錯誤?這對我來說沒有意義。
檢查答案:http://stackoverflow.com/questions/36921951/truth-value-of-a-series-is-ambiguous-use-a-empty -a-bool-a-item-a-any-o – Rohanil
好的,謝謝。因此,如果我使用 pd.isnull(df_gen [[customer_ID]]。loc [ts] .item()),那麼該布爾值被評估爲OK,但我不明白爲什麼原始文件不起作用。 – doctorer
由於原始返回''對象,它不是布爾值。 –
Rohanil