2017-04-05 76 views
1

我收到錯誤時,我對數據框中的單個元素進行比較,但我不明白爲什麼。熊貓布爾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表達式中生成一個錯誤?這對我來說沒有意義。

+0

檢查答案:http://stackoverflow.com/questions/36921951/truth-value-of-a-series-is-ambiguous-use-a-empty -a-bool-a-item-a-any-o – Rohanil

+0

好的,謝謝。因此,如果我使用 pd.isnull(df_gen [[customer_ID]]。loc [ts] .item()),那麼該布爾值被評估爲OK,但我不明白爲什麼原始文件不起作用。 – doctorer

+0

由於原始返回''對象,它不是布爾值。 – Rohanil

回答

0

第二組[]正在返回一個我誤認爲單個值的系列。最簡單的解決方法是刪除[]:這

if pd.isnull(df[customer_ID].loc[ts]): 
     pass 
2

問題是你需要返回標(TrueFalse)比較標量,但有一個項目Series,將其轉化爲一個項目boolean Series

解決方案是採用Series.itemvalues與選擇第一個值轉換爲標量由[0]

customer_ID = '8143511' 
ts = '2012-07-01 00:00:00' 

print (df[[customer_ID]].loc[ts].item()) 
nan 

if pd.isnull(df[[customer_ID]].loc[ts]).item(): 
    print ('super') 
print (df[[customer_ID]].loc[ts].values[0]) 
nan 

if pd.isnull(df[[customer_ID]].loc[ts]).values[0]: 
    print ('super') 

但如果使用DataFrame.loc,得到scalar(如果不重複索引或列名):

print (df.loc[ts, customer_ID]) 
nan 

customer_ID = '8143511' 
ts = '2012-07-01 00:00:00' 
if pd.isnull(df.loc[ts, customer_ID]): 
    print ('super') 
+0

謝謝。這讓我意識到第二套'[]'是造成這個問題的原因,所以這些解決方案的工作原理很簡單,但是移除方括號就更簡單了。 – doctorer

+0

很高興能爲您提供幫助。祝你好運! – jezrael

4

問題李es在if聲明中。

當你的代碼

if this: 
    print(that) 

thisbool(this)進行評估。而且更好的回來爲TrueFalse

但是,你這樣做:

if pd.isnull(df[[customer_ID]].loc[ts]): 
    pass # idk what you did here because you didn't say... but doesn't matter 

而且,你說pd.isnull(df[[customer_ID]].loc[ts])評價:

8143511 True 
Name: 2012-07-01 00:00:00, dtype: bool 

不會看起來像TrueFalse
bool(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.Series不能評價然而作爲TrueFalse

是,,的True S和Falsepd.Series

而這就是爲什麼它不起作用。

+0

實際上,我學到的教訓是'df [[customer_ID]]。loc [ts]'返回一個'pd.Series'不是一個單獨的值 – doctorer