简体   繁体   English

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

[英]Pandas - change the order of levels of factor-type object

I have a Pandas dataframe df with column school as factor 我有一个Pandas数据帧df ,列school作为因素

Name    school
A       An
B       Bn
C       Bn

How can I change the levels of the school column from ('An', 'Bn') to ('Bn', 'An') in python? 如何在python中将school列的级别从('An','Bn')更改为('Bn','An')?

R equivalent is R等价物

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

You can use reorder_categories (you pass in the sorted factors): 您可以使用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]

You can do this inplace: 您可以在现场执行此操作:

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 . 请参阅文档的重新排序类别部分

You can set cat.categories : 你可以设置cat.categories

import pandas as pd

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

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

As a general solution, you can remap using a dictionary: 作为一般解决方案,您可以使用字典重新映射:

df = pd.DataFrame({'Name': ['A', 'B', 'C'], 
                   'school': ['An', 'Bn', 'Bn']})
d = {'An': 'Bn', 'Bn': 'An'}
df['school'] = df.school.map(d)
>>> df
  Name school
0    A     Bn
1    B     An
2    C     An

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM