Ordering pandas DataFrame Rows by Multiple Columns in Python (Example Code)
In this article, I’ll demonstrate how to sort the rows of a pandas DataFrame based on multiple variables in the Python programming language.
Setting up the Example
import pandas as pd # Import pandas library to Python |
import pandas as pd # Import pandas library to Python
my_df = pd.DataFrame({'A':[5, 5, 5, 1, 2, 8], # Construct example DataFrame in Python 'B':['b', 'a', 'a', 'a', 'c', 'b'], 'C':['a', 'a', 'c', 'b', 'a', 'c']}) print(my_df) # Display example DataFrame in console # A B C # 0 5 b a # 1 5 a a # 2 5 a c # 3 1 a b # 4 2 c a # 5 8 b c |
my_df = pd.DataFrame({'A':[5, 5, 5, 1, 2, 8], # Construct example DataFrame in Python 'B':['b', 'a', 'a', 'a', 'c', 'b'], 'C':['a', 'a', 'c', 'b', 'a', 'c']}) print(my_df) # Display example DataFrame in console # A B C # 0 5 b a # 1 5 a a # 2 5 a c # 3 1 a b # 4 2 c a # 5 8 b c
Example: How to Sort pandas DataFrame by Multiple Columns
my_df = my_df.sort_values(['B', 'C']) # Apply sort_values function print(my_df) # Display updated DataFrame # A B C # 1 5 a a # 3 1 a b # 2 5 a c # 0 5 b a # 5 8 b c # 4 2 c a |
my_df = my_df.sort_values(['B', 'C']) # Apply sort_values function print(my_df) # Display updated DataFrame # A B C # 1 5 a a # 3 1 a b # 2 5 a c # 0 5 b a # 5 8 b c # 4 2 c a