Pandas DataFrame sort_values()

We can arrange data in order by different columns by using sort_values().

By default it is in the ascending order. We will return in ascending order of MATH column.
import pandas as pd 
my_dict={'NAME':['Ravi','Raju','Alex','Ron','King','Jack'],
         'ID':[1,2,3,4,5,6],
         'MATH':[80,40,70,70,70,30],
         'ENGLISH':[80,70,40,50,60,30]}
my_data = pd.DataFrame(data=my_dict)
print(my_data.sort_values(by=['MATH']))
Output
   NAME  ID  MATH  ENGLISH
5  Jack   6    30       30
1  Raju   2    40       70
2  Alex   3    70       40
3   Ron   4    70       50
4  King   5    70       60
0  Ravi   1    80       80

Using two columns

print(my_data.sort_values(by=['MATH','ENGLISH']))
Output is here
   NAME  ID  MATH  ENGLISH
5  Jack   6    30       30
1  Raju   2    40       70
2  Alex   3    70       40
3   Ron   4    70       50
4  King   5    70       60
0  Ravi   1    80       80
We can display in Descending order.
print(my_data.sort_values(by=['MATH'],ascending=False))
Output is here
   NAME  ID  MATH  ENGLISH
0  Ravi   1    80       80
2  Alex   3    70       40
3   Ron   4    70       50
4  King   5    70       60
1  Raju   2    40       70
5  Jack   6    30       30

Using string column

print(my_data.sort_values(by=['NAME'],ascending=False))
Output
   NAME  ID  MATH  ENGLISH
3   Ron   4    70       50
0  Ravi   1    80       80
1  Raju   2    40       70
4  King   5    70       60
5  Jack   6    30       30
2  Alex   3    70       40
Pandas Plotting graphs Filtering of Data
Subhendu Mohapatra — author at plus2net
Subhendu Mohapatra

Author

🎥 Join me live on YouTube

Passionate about coding and teaching, I publish practical tutorials on PHP, Python, JavaScript, SQL, and web development. My goal is to make learning simple, engaging, and project‑oriented with real examples and source code.



Subscribe to our YouTube Channel here



plus2net.com







Python Video Tutorials
Python SQLite Video Tutorials
Python MySQL Video Tutorials
Python Tkinter Video Tutorials
We use cookies to improve your browsing experience. . Learn more
HTML MySQL PHP JavaScript ASP Photoshop Articles Contact us
©2000-2025   plus2net.com   All rights reserved worldwide Privacy Policy Disclaimer