該錯誤指示您是在numpy的陣列或大熊貓系列使用這樣的邏輯運算符爲not, and, or
從基地蟒:
例如:
s = pd.Series([1,1,2,2])
not pd.isnull(s.mode())
給出相同的錯誤:
ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().
如果你看堆棧跟蹤,錯誤來自這一行:
fillna(self, value, method, limit)
1465 else:
1466
-> 1467 if not isnull(value) and value not in self.categories:
1468 raise ValueError("fill value must be in categories")
1469
因此,它正在檢查您試圖填充的值是否在類別中;並且該行要求該值爲標量以便與not
和and
兼容;然而,series.mode()
總是返回一個系列,它失敗這條線,嘗試從mode()
獲取價值並填寫:
train['Married']=train['Married'].fillna(train['Married'].mode().iloc[0])
工作的示例:
s = pd.Series(["YES", "NO", "YES", "YES", None])
s1 = s.astype('category')
s1.cat.categories = [0, 1]
s1
#0 1.0
#1 0.0
#2 1.0
#3 1.0
#4 NaN
#dtype: category
#Categories (2, int64): [0, 1]
s1.fillna(s1.mode().iloc[0])
#0 1
#1 0
#2 1
#3 1
#4 1
#dtype: category
#Categories (2, int64): [0, 1]
你能分手的計算來看看這個錯誤是由於'.mode()','.fillna()'還是'='歸因? –