ValueError : Cannot setitem on a Categorical with a new category, set the categories first
ValueError : Cannot setitem on a Categorical with a new category, set the categories first
我有一个值从 0 到 600 变化的列,我想将 0 到 9.2 的值按 0.4 的增量分组,并将 9.2 到 600 值之间的 1 组分组,因为 outlier.I 尝试了以下代码;
bin_labels = ['0-0.4', '0.4-0.8', '0.8-1.2', '1.2-1.6',
'1.6-2.0', '2.0-2.4','2.4-2.8', '2.8-3.2',
'3.2-3.6', '3.6-4.0','4.0-4.4', '4.4-4.8',
'4.8-5.2', '5.2-5.6','5.6-6.0', '6.0-6.4',
'6.4-6.8', '6.8-7.2','7.2-7.6', '7.6-8.0',
'8.0-8.4', '8.4-8.8','8.8-9.2']
bins = np.linspace(0.0,9.2,24)
df['A_group'] = pd.cut(df['A'], bins = bins, labels = bin_labels, include_lowest = True)
之后,我想使用以下代码用“9.2-more”标签值填充 9.2 到 600 之间的值;
df['A_group'] = df['A_group'].fillna('9.2-more')
但是提示如下错误;
Cannot setitem on a Categorical with a new category, set the categories first
您可以将 float("inf")
附加到 bins
并在 bin_labels
中包含“9.2-more”:
bin_labels = [ '0-0.4', '0.4-0.8', '0.8-1.2', '1.2-1.6',
'1.6-2.0', '2.0-2.4', '2.4-2.8', '2.8-3.2',
'3.2-3.6', '3.6-4.0', '4.0-4.4', '4.4-4.8',
'4.8-5.2', '5.2-5.6', '5.6-6.0', '6.0-6.4',
'6.4-6.8', '6.8-7.2', '7.2-7.6', '7.6-8.0',
'8.0-8.4', '8.4-8.8', '8.8-9.2', "9.20-more"]
bins = np.append(np.linspace(0.0, 9.2, 24), float("inf"))
df["A_group"] = pd.cut(df['A'], bins = bins, labels = bin_labels, include_lowest = True)
我有一个值从 0 到 600 变化的列,我想将 0 到 9.2 的值按 0.4 的增量分组,并将 9.2 到 600 值之间的 1 组分组,因为 outlier.I 尝试了以下代码;
bin_labels = ['0-0.4', '0.4-0.8', '0.8-1.2', '1.2-1.6',
'1.6-2.0', '2.0-2.4','2.4-2.8', '2.8-3.2',
'3.2-3.6', '3.6-4.0','4.0-4.4', '4.4-4.8',
'4.8-5.2', '5.2-5.6','5.6-6.0', '6.0-6.4',
'6.4-6.8', '6.8-7.2','7.2-7.6', '7.6-8.0',
'8.0-8.4', '8.4-8.8','8.8-9.2']
bins = np.linspace(0.0,9.2,24)
df['A_group'] = pd.cut(df['A'], bins = bins, labels = bin_labels, include_lowest = True)
之后,我想使用以下代码用“9.2-more”标签值填充 9.2 到 600 之间的值;
df['A_group'] = df['A_group'].fillna('9.2-more')
但是提示如下错误;
Cannot setitem on a Categorical with a new category, set the categories first
您可以将 float("inf")
附加到 bins
并在 bin_labels
中包含“9.2-more”:
bin_labels = [ '0-0.4', '0.4-0.8', '0.8-1.2', '1.2-1.6',
'1.6-2.0', '2.0-2.4', '2.4-2.8', '2.8-3.2',
'3.2-3.6', '3.6-4.0', '4.0-4.4', '4.4-4.8',
'4.8-5.2', '5.2-5.6', '5.6-6.0', '6.0-6.4',
'6.4-6.8', '6.8-7.2', '7.2-7.6', '7.6-8.0',
'8.0-8.4', '8.4-8.8', '8.8-9.2', "9.20-more"]
bins = np.append(np.linspace(0.0, 9.2, 24), float("inf"))
df["A_group"] = pd.cut(df['A'], bins = bins, labels = bin_labels, include_lowest = True)