What is sort_values() in pandas?
Pandas is a Python library that is used in manipulating and analyzing data. This data exists in the form of two-dimensional structures called data frames. These data frames make the organization of data straightforward and allows easy understanding of the tabular format. To further increase our comprehension of the data, the sort_values() function sorts the data frame in ascending or descending order of the column passed as an argument. For example, suppose you want data on your students based on increasing age. You could do this easily by using the sort_values() function and passing the column with the students’ ages as an argument.
Syntax
dataframe.sort_values(by, axis, ascending, inplace, kind, na_position, ignore_index, key)
Arguments
by: Specifies the column or index label to sort by. Datatype must be a string.axis: Specifies the axis by which to sort.1for column or0for index. (Optional)ascending: If set toTrue, data frame will be sorted in ascending manner.Trueis the default. (Optional)inplace: If set toTrue, operation will be performed on the original data frame.Falseis the default. (Optional)kind: Specifies the sorting algorithm to use. The options aremergesort,quicksort, orheapsort.quicksortis the default. (Optional)na_position: Specifies where to putNULLvalues. Options arelastorfirst.lastis the default. (Optional)ignore_index: If set to True, index is ignored.Falseis the default. (Optional)key: Specifies a function to be executed before the data is sorted. (Optional)
Return Value
A sorted data frame object will be returned.
Code Example
#import libraryimport pandas as pd#initialize the datadata = {"Name": ['John', 'Kelly', 'Kris', 'Betty', 'Bob'],"Age": [16, 19, 15, 21, 17],"Marks": [89, 76, 85, 67, 53]}#create a dataframe objectdf = pd.DataFrame(data)#print the data frameprint(df)
To sort the data according to increasing number of marks, we use the sort_values() function with column Age passed as the parameter.
#sort the data framesorted_df = df.sort_values(by='Marks')#print the sorted data frameprint(sorted_df)