Pandas DataFrame is two-dimensional size-mutable, potentially heterogeneous tabular data structure with labeled axes (rows and columns). We often need to do certain operations on both rows and column while handling the data.
Let’s see how to sort rows in pandas DataFrame.
Code #1: Sorting rows by Science
Sorting rows by Science: English Maths Science name 1 4 5 9 Marsh 0 7 8 7 Simon 4 8 7 7 Selena 2 7 6 5 Gaurav 3 6 9 4 Alex
Code #2: Sort rows by Maths and then by English.
Sort rows by Maths and then by English: English Maths Science name 1 4 5 9 Marsh 2 7 6 5 Gaurav 4 8 7 7 Selena 0 7 8 7 Simon 3 6 9 4 Alex
Code #3: If you want missing values first.
English Maths Science name 3 6 9 4 Alex 2 7 6 5 Gaurav 0 7 8 7 Simon 4 8 7 7 Selena 1 4 5 9 Marsh
As there are no missing values in this example this will produce same output as the above one, but sorted in ascending order.
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.
- Python | Delete rows/columns from DataFrame using Pandas.drop()
- How to randomly select rows from Pandas DataFrame
- How to get rows/index names in Pandas dataframe
- Get all rows in a Pandas DataFrame containing given substring
- Different ways to iterate over rows in Pandas Dataframe
- Selecting rows in pandas DataFrame based on conditions
- How to iterate over rows in Pandas Dataframe
- Dealing with Rows and Columns in Pandas DataFrame
- Iterating over rows and columns in Pandas DataFrame
- Ranking Rows of Pandas DataFrame
- Create a list from rows in Pandas dataframe
- Create a list from rows in Pandas DataFrame | Set 2
- How to Drop Rows with NaN Values in Pandas DataFrame?
- Drop rows from Pandas dataframe with missing values or NaN in columns
- How to drop rows in Pandas DataFrame by index labels?
- Get the number of rows and number of columns in Pandas Dataframe
- How to create an empty DataFrame and append rows & columns to it in Pandas?
- Apply a function to single or selected columns or rows in Pandas Dataframe
- Find maximum values & position in columns and rows of a Dataframe in Pandas
- Select first or last N rows in a Dataframe using head() and tail() method in Python-Pandas
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.