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')
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.
You can set cat.categories
:
import pandas as pd
school = pd.Series(["An", "Bn", "Bn"])
school = school.astype("category")
school.cat.categories = ["Bn", "An"]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With