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.
- Get all rows in a Pandas DataFrame containing given substring
- How to iterate over rows in Pandas Dataframe
- Ranking Rows of Pandas DataFrame
- Different ways to iterate over rows in Pandas Dataframe
- Iterating over rows and columns in Pandas DataFrame
- Create a list from rows in Pandas DataFrame | Set 2
- How to get rows/index names in Pandas dataframe
- Create a list from rows in Pandas dataframe
- How to randomly select rows from Pandas DataFrame
- Dealing with Rows and Columns in Pandas DataFrame
- Selecting rows in pandas DataFrame based on conditions
- Python | Delete rows/columns from DataFrame using Pandas.drop()
- Python | Pandas DataFrame.fillna() to replace Null values in dataframe
- Pandas Dataframe.to_numpy() - Convert dataframe to Numpy array
- Drop rows from the dataframe based on certain condition applied on a column
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. 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.