2017-07-27 91 views
2

我的数据包含一列“已婚”,其具有分类值是或否。我把它改为数值类型:为什么大熊猫的分类DataFrame会给出真值误差?

train['Married']=train['Married'].astype('category') 
train['Married'].cat.categories=[0,1] 

现在我用下面的代码填写缺失值:

train['Married']=train['Married'].fillna(train['Married'].mode()) 

这是给错误:

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

有人可以解释为什么吗?

+0

你能分手的计算来看看这个错误是由于'.mode()','.fillna()'还是'='归因? –

回答

3

该错误指示您是在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 

因此,它正在检查您试图填充的值是否在类别中;并且该行要求该值为标量以便与notand兼容;然而,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]