{"id":26225,"date":"2022-01-03T09:13:25","date_gmt":"2022-01-03T03:43:25","guid":{"rendered":"https:\/\/python-programs.com\/?p=26225"},"modified":"2022-01-03T09:13:25","modified_gmt":"2022-01-03T03:43:25","slug":"python-program-for-sorting-a-dataframe","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-program-for-sorting-a-dataframe\/","title":{"rendered":"Python Program for Sorting a Dataframe"},"content":{"rendered":"

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.<\/p>\n

Dataframe:<\/strong><\/p>\n

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.<\/p>\n

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.<\/p>\n

Sorting a Dataframe<\/h2>\n

sort_values() Method:<\/strong><\/p>\n

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.<\/p>\n

Syntax:<\/strong><\/p>\n

pandas.DataFrame.sort_values(by, axis=0, ascending=True, kind=\u2019mergesort\u2019)<\/pre>\n

Parameters<\/strong><\/p>\n

by:<\/strong> It is the list of columns to be sorted.<\/p>\n

axis<\/strong>: axis= 1 represents column-wise operations and 0 represents row-wise operations.<\/p>\n

ascending:<\/strong> If It is set to True, the dataframe is sorted in ascending order.<\/p>\n

kind:<\/strong> It has three possible values: ‘Quicksort, mergesort, or heapsort.’<\/p>\n

Example1:<\/strong><\/p>\n

Approach:<\/strong><\/p>\n