pandas 熊猫分类错误:“无法在具有新类别的分类上设置项目,请先设置类别”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/49197386/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
pandas Categorical error: "Cannot setitem on a Categorical with a new category, set the categories first"
提问by Gabriela M
I have the following df data frame in pandas:
我在Pandas中有以下 df 数据框:
weekday venta_total_cy
0 Viernes 5.430211e+09
1 Lunes 3.425554e+09
2 Sabado 6.833202e+09
3 Domingo 6.566466e+09
4 Jueves 2.748710e+09
5 Martes 3.328418e+09
6 Miercoles 3.136277e+09
What I want to do is to order the data frame by the following days' order:
我想要做的是按以下几天的顺序对数据框进行排序:
weekday
Lunes
Martes
Miercoles
Jueves
Viernes
Sabado
Domingo
To do so, I used the following code:
为此,我使用了以下代码:
df['weekday'] = pd.Categorical(df[['weekday']], categories=["Lunes", "Martes", "Miercoles", "Jueves", "Viernes", "Sabado", "Domingo"])
When I run the code, I get this error:
当我运行代码时,我收到此错误:
ValueError: Cannot setitem on a Categorical with a new category, set the categories first
I have not found enough documentation to resolve this. Can you help me? Thanks!
我还没有找到足够的文档来解决这个问题。你能帮助我吗?谢谢!
采纳答案by cs95
df[['weekday']]
returns a dataframe, which is incorrect. Convert the seriescolumn to categorical instead. Also, use the ordered=True
argument to establish order in your categorical column.
df[['weekday']]
返回一个数据帧,这是不正确的。将系列列转换为分类列。此外,使用ordered=True
参数在您的分类列中建立顺序。
categories = np.array(
['Lunes', 'Martes', 'Miercoles', 'Jueves', 'Viernes', 'Sabado', 'Domingo']
)
df.weekday = pd.Categorical(df.weekday, categories=categories, ordered=True)
df.sort_values(by='weekday')
weekday venta_total_cy
1 Lunes 3.425554e+09
5 Martes 3.328418e+09
6 Miercoles 3.136277e+09
4 Jueves 2.748710e+09
0 Viernes 5.430211e+09
2 Sabado 6.833202e+09
3 Domingo 6.566466e+09