Python Program for Sorting a Dataframe

Python provides us with a number of data structures through which we can interact with data and perform operations on it. Particularly when it comes to data science and analysis, the data structures provided by Python have given shape to the processing it.

Dataframe:

Python provides one such data structure, DataFrame. It saves data in the form of rows and columns. The datasets can be analyzed within the environment. These synchronized rows and columns are ready for data preprocessing and manipulation.

The Python Pandas module provides a data structure called a DataFrame. It organizes data into rows and columns and stores it. As a result, we can have the data in the form of a matrix, with the entities represented as rows and columns.

Sorting a Dataframe

sort_values() Method:

The sort_values() function in Pandas sorts a dataframe in the ascending or descending order of the passed Column. It differs from the sorted Python function in that it cannot sort a data frame and no specific column can be selected.

Syntax:

pandas.DataFrame.sort_values(by, axis=0, ascending=True, kind=’mergesort’)

Parameters

by: It is the list of columns to be sorted.

axis: axis= 1 represents column-wise operations and 0 represents row-wise operations.

ascending: If It is set to True, the dataframe is sorted in ascending order.

kind: It has three possible values: ‘Quicksort, mergesort, or heapsort.’

Example1:

Approach:

  • Import pandas module using the import keyword.
  • Give some random list of data(as dictionary) and store it in a variable.
  • Pass the given data to the DataFrame() function and store it in another variable.
  • Print the above result.
  • Pass the column name, axis=0, and ascending = True as the arguments to the sort_values() function to sort the salary column values in Ascending Order.
  • Print the data after sorting the salary column in Ascending Order.
  • The Exit of the Program.

Below is the implementation:

# Import pandas module using the import keyword
import pandas as pd
# Give some random list of data(as dictionary) and store it in a variable
gvn_data = {"ID": [11, 12, 13, 14, 15, 16], "Name": ["peter", "irfan", "mary",
                                                     "riya", "virat", "sunny"], "salary": [10000, 25000, 15000, 50000, 30000, 22000]}
# Pass the given data to the DataFrame() function and store it in another variable
block_data = pd.DataFrame(gvn_data)
# Print the above result.
print("The given input Dataframe: ")
print(block_data)
print()
# Pass the column name, axis=0 and ascending = True as the arguments to the sort_values()
# function to sort the salary column values in Ascending Order
sortd_saly = block_data.sort_values('salary', axis = 0, ascending = True)
# Print the data after sorting the salary column in Ascending Order.
print("The given data after sorting the salary column in Ascending Order:")
print(sortd_saly)

Output:

The given input Dataframe: 
   ID   Name  salary
0  11  peter   10000
1  12  irfan   25000
2  13   mary   15000
3  14   riya   50000
4  15  virat   30000
5  16  sunny   22000

The given data after sorting the salary column in Ascending Order:
   ID   Name  salary
0  11  peter   10000
2  13   mary   15000
5  16  sunny   22000
1  12  irfan   25000
4  15  virat   30000
3  14   riya   50000

Example2:

Here we sort the two columns ID and salary in descending order.

# Import pandas module using the import keyword
import pandas as pd
# Give some random list of data(as dictionary) and store it in a variable
gvn_data = {"ID": [11, 12, 13, 14, 15, 16], "Name": ["peter", "irfan", "mary",
                                                     "riya", "virat", "sunny"], "salary": [10000, 25000, 15000, 50000, 30000, 22000]}
# Pass the given data to the DataFrame() function and store it in another variable
block_data = pd.DataFrame(gvn_data)
# Print the above result.
print("The given input Dataframe: ")
print(block_data)
print()
# Pass the list of columnnames('ID','salary'), axis=0 and ascending = False as the arguments to the sort_values()
# function to sort the 'ID','salary' columns values in Descending Order
sortd_data = block_data.sort_values(['ID','salary'], axis = 0, ascending = False)
# Print the given data after sorting the ('ID','salary') columns in Descending Order.
print("The given data after sorting the ('ID','salary') columns in Descending Order:")
print(sortd_data)

Output:

The given input Dataframe: 
   ID   Name  salary
0  11  peter   10000
1  12  irfan   25000
2  13   mary   15000
3  14   riya   50000
4  15  virat   30000
5  16  sunny   22000

The given data after sorting the ('ID','salary') columns in Descending Order:
   ID   Name  salary
5  16  sunny   22000
4  15  virat   30000
3  14   riya   50000
2  13   mary   15000
1  12  irfan   25000
0  11  peter   10000