Saturday, June 22, 2024
HomejQuery5 Examples of Sorting Pandas Knowledge Body by sort_values methodology

5 Examples of Sorting Pandas Knowledge Body by sort_values methodology

Easy methods to Kind Knowledge Frames in Pandas

Pandas knowledge body has a sort_values methodology that’s used to kind the info body’s knowledge in ascending or descending order.

The sorting may be primarily based on column labels.

The sort_values methodology has completely different parameters as proven within the syntax under.


DataFrame.sort_values(by, *, axis=0, ascending=True, inplace=False, sort=’quicksort’, na_position=’final’, ignore_index=False, key=None)

Allow us to present you examples of utilizing sort_values with its parameters under.

An instance of sorting outcomes by a column label in ascending order

Within the Python program under, we created an information body with three columns and 5 rows of knowledge.

Then we displayed the info body in its unique kind.

That is adopted through the use of the sort_values methodology the place we solely specified the column label to kind the outcomes.

Python program:


You’ll be able to see, the outcomes are sorted by Worker names within the knowledge body.

Sorting knowledge body in descending order

As it may be seen within the syntax, the default worth for sorting order is ascending i.e.


Through the use of the False worth, chances are you’ll get the info sorted in descending order.

See the instance under the place we used the identical knowledge body and sorted leads to descending order.




The instance of sorting by wage column

The instance under kinds the lead to descending order primarily based on the wage column:


Kind outcome by two columns instance

You may additionally kind the outcomes by offering two or extra columns.

The instance under kinds the outcomes by identify and wage columns in our instance knowledge body:


Utilizing ignore_index parameter instance

By default, the ignore_index= False. Which means the index column numbers stay in place after sorting the outcomes. Within the above instance output, you’ll be able to see 3, 4, 0, 1, 2, and the numbers which might be for the unique DF.

In the event you might ignore_index=True then the ensuing axis after sorting is labeled as 0,1, 2…

See the distinction within the instance under:





Please enter your comment!
Please enter your name here

Most Popular

Recent Comments