我正在尝试使用以下代码填充缺失值(NAN)
NAN_SUBSTITUTION_VALUE = 1
g = g.fillna(NAN_SUBSTITUTION_VALUE)
但是我遇到了以下错误
ValueError: fill value must be in categories.
请问有人能够解释一下这个错误吗?
你的问题忽略了一个重要的点,那就是 g
是什么,特别是它的数据类型是categorical
。我猜想它大概是这样的:
你的问题忽略了一个重要的点,那就是 g
是什么,特别是它的数据类型是categorical
。我猜想它大概是这样的:
g = pd.Series(["A", "B", "C", np.nan], dtype="category")
你遇到的问题是 fillna
需要一个已经存在的类别值。例如,g.fillna("A")
可以运行,但是 g.fillna("D")
不行。如果你想用一个新值填充序列,可以这样做:
g_without_nan = g.cat.add_categories("D").fillna("D")
g = g.cat.add_categories([1])
g.fillna(1)
g[cat_column_name] = g[cat_column_name].cat.add_categories([1])
。如果类别已经排序,则添加的类别将作为最大值。 - Gaduks创建完分类数据后,只能插入属于该类别的值。
>>> df
ID value
0 0 20
1 1 43
2 2 45
>>> df["cat"] = df["value"].astype("category")
>>> df
ID value cat
0 0 20 20
1 1 43 43
2 2 45 45
>>> df.loc[1, "cat"] = np.nan
>>> df
ID value cat
0 0 20 20
1 1 43 NaN
2 2 45 45
>>> df.fillna(1)
ValueError: fill value must be in categories
>>> df.fillna(43)
ID value cat
0 0 20 20
1 1 43 43
2 2 45 45
正如许多人之前所说,此错误来自于该特性的类型为“category”。
我建议首先将其转换为字符串,使用fillna,最后如果需要,再将其转换回类别。
g = g.astype('string')
g = g.fillna(NAN_SUBSTITUTION_VALUE)
g = g.astype('category')
#creates a random permuation of the categorical values
permutation = np.random.permutation(df[field])
#erase the empty values
empty_is = np.where(permutation == "")
permutation = np.delete(permutation, empty_is)
#replace all empty values of the dataframe[field]
end = len(permutation)
df[field] = df[field].apply(lambda x: permutation[np.random.randint(end)] if pd.isnull(x) else x)
它的工作效率非常高。
深入理解是因为:
分类变量只能取有限的、通常是固定的可能值(类别)。与统计分类变量相反,分类变量可能具有顺序,但不能进行数值运算(加法、除法等)。
分类变量的所有值都在类别或 np.nan 中。分配类别之外的值将引发 ValueError。顺序由类别的顺序定义,而不是值的词汇顺序。
https://pandas.pydata.org/docs/reference/api/pandas.Categorical.html
one = pd.Series(["A", "B", "C", None], dtype="category")
two = pd.Series(["A", "C", "B", "D"], dtype="category")
combined_categories = pd.concat([one.dropna(), two.dropna()]).unique()
one = one.cat.set_categories(combined_categories)
two = two.cat.set_categories(combined_categories)
result = one.fillna(two)