Sort the Pandas DataFrame by two or more columns Last Updated : 17 Aug, 2020 Comments Improve Suggest changes Like Article Like Report In this article, our basic task is to sort the data frame based on two or more columns. For this, Dataframe.sort_values() method is used. This method sorts the data frame in Ascending or Descending order according to the columns passed inside the function. First, Let's Create a Dataframe: Python3 #import python library import pandas as pd # dictionary data_frame = { 'name': ['Akash Kumar', 'Diksha Tewari', 'Bhawna Gourh', 'Ayush Sharma'], 'age': [20, 21, 22, 23], 'favorite_color': ['black', 'Yellow', 'Pink', "Orange"], 'grade': [88, 92, 95, 70] } # create data frame with indexing df = pd.DataFrame(data_frame, index = [1, 2, 3, 4]) # printing the dataframe df Output: Example 1: Sort Dataframe based on 'age'(in descending order) and 'grade' (in ascending order) column. Python3 # sort the dataframe # based on age and grade df.sort_values(['age', 'grade'], ascending = [False, True]) Output: Example 2: Sort Dataframe based on 'name' and 'favorite_color' column in ascending order. Python3 # sort the dataframe based # on name and favorite_colr df.sort_values(['name', 'favorite_color'], ascending=[True, True]) Output: Example 3: In-place sorting of Dataframe based on 'grade' and 'favorite_color' column. In case of in-place sorting, Dataframe.sort_values() method returns nothing it performs changes in the actual dataframe. Python3 df.sort_values(["grade", "favorite_color"], axis = 0, ascending = True, inplace = True, na_position ='first') # printing the dataframe df Output: Comment More infoAdvertise with us Next Article Sort the Pandas DataFrame by two or more columns A Akashkumar17 Follow Improve Article Tags : Python Python-pandas Python pandas-dataFrame pandas-dataframe-program Practice Tags : python Similar Reads How to sort a Pandas DataFrame by multiple columns? We are given a DataFrame and our task is to sort it based on multiple columns. This means organizing the data first by one column and then by another within that sorted order. For example, if we want to sort by 'Rank' in ascending order and then by 'Age' in descending order, the output will be a Dat 4 min read How to reverse the column order of the Pandas DataFrame? Let's learn how to reverse the column order of the Pandas DataFrame using iloc and other approaches. Using Slicing - columns[::-1]The columns[::-1] slices the column labels in reverse order. Let's consider an example: Pythonimport pandas as pd # Sample DataFrame data = { 'Name': ['Rachel', 'Monica', 3 min read Merge two Pandas DataFrames on certain columns Let's learn how to merge two Pandas DataFrames on certain columns using merge function. The merge function in Pandas is used to combine two DataFrames based on a common column or index. merge Function Syntax: DataFrame.merge(right, how='inner', on=None, left_on=None, right_on=None, left_index=False, 3 min read Sort Rows or Columns in Pandas Dataframe Based on Values Sorting is a fundamental operation when working with data in Pandas. Whether you need to arrange rows in ascending or descending order or reorder columns based on values, Pandas provides powerful functions to make sorting easy and efficient. In this article, we'll explore different ways to sort rows 4 min read Split a text column into two columns in Pandas DataFrame Let's see how to split a text column into two columns in Pandas DataFrame. Method #1 : Using Series.str.split() functions. Split Name column into two different columns. By default splitting is done on the basis of single space by str.split() function. Python3 # import Pandas as pd import pandas as p 3 min read Like