How to Sort a Data Frame by a Column in Pandas

Channel:
Subscribers:
44,100
Published on ● Video Link: https://www.youtube.com/watch?v=Zs_C41bSw3o



Category:
Guide
Duration: 2:32
2,551 views
45


↓ Code Available Below! ↓

This video shows how to sort the rows of a pandas data frame by the values of a column. Sorting or ordering data by the values in a particular column can be a useful way to rearrange data in a logical way and allow you to focus on data records that are at the extremes of a certain variable.

If you find this video useful, like, share and subscribe to support the channel!
► Subscribe: https://www.youtube.com/c/DataDaft?sub_confirmation=1

Code used in this Python Code Clip:

import pandas as pd

import statsmodels.api as sm #(To access mtcars dataset)
mtcars = sm.datasets.get_rdataset("mtcars", "datasets", cache=True).data

mtcars.head()

# Sort a data frame by a column with df.sort_values()

mtcars.sort_values(by = ["mpg"], # Row or columns names to sort by
axis = 0, # Sort Rows axis = 0
ascending=True) # Sort ascending or descending?

# Sort by multiple columns

mtcars.sort_values(by = ["cyl", "mpg"], # Multiple column sorting
axis = 0, # Sort rows; axis = 0
ascending=[True, False]) # Sort ascending or descending?


** Note: YouTube does not allow greater than or less than symbols in the text description, so the code above will not be exactly the same as the code shown in the video! I will use Unicode large < and > symbols in place of the standard sized ones. .


⭐ Kite is a free AI-powered coding assistant that integrates with popular editors and IDEs to give you smart code completions and docs while you’re typing. It is a cool application of machine learning that can also help you code faster! Check it out here: https://www.kite.com/get-kite/?utm_medium=referral&utm_source=youtube&utm_campaign=datadaft&utm_content=description-only







Tags:
pandas sort by column
pandas sort rows
pandas sort data frame
sort pandas data frame
sort data in python
pandas sort_values()
pandas sort_values
pandas sort by multiple columns
data frame sort
pandas sorting
data frame sort rows by a column
pandas sort rows by a column
python sort data frame by a column
python data sort
pandas data frame sort
pandas dataframe sort
dataframe sort
pandas order data frame
pandas reorder data frame
pandas arrange data frame