Ajay
Ajay

Reputation: 164

How to reorder some rows in specfic orders in pandas python

I have a dataset like this:

     A    B
0    2    3
1    2    5
2    1    7
3    1    8

And I want to re-order it like this in a fast and efficient way:

     A    B
0    1    8
1    1    7
2    2    5
3    2    3

How can I achieve this efficiently?

Upvotes: 0

Views: 593

Answers (2)

user-116
user-116

Reputation: 121

You could try the implementation given on this page.using the sort_values function. Assuming your data is stored in a data frame you can use `df.sort_values(by="B") For a better understanding go to the following link

https://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.sort_values.html

Please check it out!

Upvotes: 0

Thierry K.
Thierry K.

Reputation: 97

According that your DataFrame is df, just use

df.sort_values(by='B')

Upvotes: 1

Related Questions