A very common task is to sort the rows of a data frame to get the best/worst values at the top. The function used to sort data is sort_values()
You can sort data by one column or multiple columns at a time.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
# Defining Employee Data import pandas as pd EmpData=pd.DataFrame({'Name': ['ram','ravi','sham','sita','gita'], 'id': [101,102,103,104,105], 'Gender': ['M','M','M','F','F'], 'Age': [21,25,24,28,25], 'ExpMonths':[1.5,2,3,12,7] }) # Priting data print(EmpData) # Sorting the data based on employee experience EmpData.sort_values(by=['ExpMonths'], ascending=True) # Sorting based on multiple columns EmpData.sort_values(by=['Age','ExpMonths'], ascending=False) |
Sample Output:

Author Details
Lead Data Scientist
Farukh is an innovator in solving industry problems using Artificial intelligence. His expertise is backed with 10 years of industry experience. Being a senior data scientist he is responsible for designing the AI/ML solution to provide maximum gains for the clients. As a thought leader, his focus is on solving the key business problems of the CPG Industry. He has worked across different domains like Telecom, Insurance, and Logistics. He has worked with global tech leaders including Infosys, IBM, and Persistent systems. His passion to teach inspired him to create this website!
