Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to reorder of rows of a dataframe based on values in a column

Tags:

python

pandas

I have a dataframe like this:

A  B  C  D
b  3  3  4
a  1  2  1
a  1  2  1
d  4  4  1
d  1  2  1
c  4  5  6

Now I hope to reorder the rows based on values in column A.

I don't want to sort the values but reorder them with a specific order like ['b', 'd', 'c', 'a'] what I expect is:

A  B  C  D
b  3  3  4
d  4  4  1
d  1  2  1
c  4  5  6
a  1  2  1
a  1  2  1
like image 980
Xin Niu Avatar asked Oct 18 '25 09:10

Xin Niu


1 Answers

This is a good use case for pd.Categorical, since you have ordered categories. Just make that column a categorical and mark ordered=True. Then, sort_values should do the rest.

df['A'] = pd.Categorical(df.A, categories=['b', 'd', 'c', 'a'], ordered=True)
df.sort_values('A')

If you want to keep your column as is, you can just use loc and the indexes.

df.loc[pd.Series(pd.Categorical(df.A, 
                                categories=['b', 'd', 'c', 'a'], 
                                ordered=True))\
         .sort_values()\
         .index\
       ]
like image 191
rafaelc Avatar answered Oct 21 '25 00:10

rafaelc