unclegood
unclegood

Reputation: 289

Arrange row in specific order

I have a data frame index (Grade) below, and it is sorted by alphabetical order. Is there a way I can decide on the arrangement such that A to H is above, followed by '2-6 mths' and '1 mth'? For columns, I know it is easy simply by df=df[[col1,col2]]

enter image description here

Upvotes: 0

Views: 1456

Answers (1)

Ted Petrou
Ted Petrou

Reputation: 62007

You almost answered your own question. You make the column of interest the index of the dataframe and then use a similar mechanism as you would for sorting columns as you would for rows except with the .loc operator.

df.loc[['A','B','C','D','E','F','G','H','2-6mths', '1 mth']]

And then you can just reset the index to move grade back as a column.

df.reset_index()

Upvotes: 2

Related Questions