为什么pandas分类DataFrame会导致真值错误?

我的数据包含一个名为’Married’的列,其值为分类数据“是”或“不是”。我将其更改为数值类型:

 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().

能有人解释一下为什么吗?


回答:

该错误表明您在numpy数组或pandas系列上使用了基本Python中的逻辑运算符,如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]

Related Posts

L1-L2正则化的不同系数

我想对网络的权重同时应用L1和L2正则化。然而,我找不…

使用scikit-learn的无监督方法将列表分类成不同组别,有没有办法?

我有一系列实例,每个实例都有一份列表,代表它所遵循的不…

f1_score metric in lightgbm

我想使用自定义指标f1_score来训练一个lgb模型…

通过相关系数矩阵进行特征选择

我在测试不同的算法时,如逻辑回归、高斯朴素贝叶斯、随机…

可以将机器学习库用于流式输入和输出吗?

已关闭。此问题需要更加聚焦。目前不接受回答。 想要改进…

在TensorFlow中,queue.dequeue_up_to()方法的用途是什么?

我对这个方法感到非常困惑,特别是当我发现这个令人费解的…

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注