Sorting rows in pandas DataFrame

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



filter_none

edit
close

play_arrow

link
brightness_4
code

# import modules
import pandas as pd
  
# create dataframe
data = {'name': ['Simon', 'Marsh', 'Gaurav', 'Alex', 'Selena'], 
        'Maths': [8, 5, 6, 9, 7], 
        'Science': [7, 9, 5, 4, 7],
        'English': [7, 4, 7, 6, 8]}
  
df = pd.DataFrame(data)
  
# Sort the dataframe’s rows by Science,
# in descending order
a = df.sort_values(by ='Science', ascending = 0)
print("Sorting rows by Science:\n \n", a)

chevron_right


Output:

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.

filter_none

edit
close

play_arrow

link
brightness_4
code

# import modules
import pandas as pd
  
# create dataframe
data = {'name': ['Simon', 'Marsh', 'Gaurav', 'Alex', 'Selena'], 
        'Maths': [8, 5, 6, 9, 7], 
        'Science': [7, 9, 5, 4, 7],
        'English': [7, 4, 7, 6, 8]}
  
df = pd.DataFrame(data)
  
# Sort the dataframe’s rows by Maths
# and then by English, in ascending order
b = df.sort_values(by =['Maths', 'English'])
print("Sort rows by Maths and then by English: \n\n", b)

chevron_right


Output:

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.

filter_none

edit
close

play_arrow

link
brightness_4
code

import pandas as pd
  
# create dataframe
data = {'name': ['Simon', 'Marsh', 'Gaurav', 'Alex', 'Selena'], 
        'Maths': [8, 5, 6, 9, 7], 
        'Science': [7, 9, 5, 4, 7],
        'English': [7, 4, 7, 6, 8]}
df = pd.DataFrame(data)
  
  
a = df.sort_values(by ='Science', na_position ='first' )
print(a)

chevron_right


Output:

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.



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.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.