Pandas - 更改因子类型对象的级别顺序

时间:2016-06-25 00:41:02

标签: python pandas

我有一个Pandas数据框df,其中列school为因素

Name    school
A       An
B       Bn
C       Bn

如何将school列的级别从(' An',' Bn')更改为(' Bn',&# 39; python中的'?

R等价物是

levels(df$school) = c('Bn','An')

3 个答案:

答案 0 :(得分:9)

您可以使用reorder_categories(传递已排序的因子):

In [11]: df
Out[11]:
  Name school
0    A     An
1    B     Bn
2    C     Bn

In [12]: df['school'] = df['school'].astype('category')

In [13]: df['school']
Out[13]:
0    An
1    Bn
2    Bn
Name: school, dtype: category
Categories (2, object): [An, Bn]

In [14]: df['school'].cat.reorder_categories(['Bn', 'An'])
Out[14]:
0    An
1    Bn
2    Bn
dtype: category
Categories (2, object): [Bn, An]

您可以在现场执行此操作:

In [21]: df['school'].cat.reorder_categories(['Bn', 'An'], inplace=True)

In [22]: df['school']
Out[22]:
0    An
1    Bn
2    Bn
Name: school, dtype: category
Categories (2, object): [Bn, An]

See the reordering categories section of the docs

答案 1 :(得分:2)

您可以设置cat.categories

import pandas as pd

school = pd.Series(["An", "Bn", "Bn"])
school = school.astype("category")

school.cat.categories = ["Bn", "An"]

答案 2 :(得分:1)

作为一般解决方案,您可以使用字典重新映射:

Promise.reject(err)