Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pandas - change the order of levels of factor-type object

Tags:

python

pandas

I have a Pandas dataframe df with column school as factor

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?

R equivalent is

levels(df$school) = c('Bn','An')
like image 861
Square9627 Avatar asked Jun 25 '16 00:06

Square9627


2 Answers

You can use reorder_categories (you pass in the sorted factors):

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.

like image 196
Andy Hayden Avatar answered Oct 14 '22 07:10

Andy Hayden


You can set cat.categories:

import pandas as pd

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

school.cat.categories = ["Bn", "An"]
like image 22
HYRY Avatar answered Oct 14 '22 07:10

HYRY