Wednesday 8 November 2023

Multi-Column Sorting in Pandas: Organizing Dataframes with Ease

In this post, I am going to explain how to sort the DataFrame by two or more columns.

I am using below data set to demonstrate the examples.

       Name  Age       City  Gender Grade
0   Krishna   34  Bangalore    Male     A
1     Sailu   35  Hyderabad  Female     B
2      Joel   29  Hyderabad    Male     C
3     Chamu   35    Chennai  Female     A
4  Jitendra   52  Bangalore    Male     B
5       Raj   34    Chennai    Male     C

  Example 1: Sort by City first and Grade next in Ascending order.

sort_by_city_and_grade_ascending = df.sort_values(['City', 'Grade'])
       Name  Age       City  Gender Grade
0   Krishna   34  Bangalore    Male     A
4  Jitendra   52  Bangalore    Male     B
3     Chamu   35    Chennai  Female     A
5       Raj   34    Chennai    Male     C
1     Sailu   35  Hyderabad  Female     B
2      Joel   29  Hyderabad    Male     C

  Example 2: Sort the DataFrame by City first and Grade next in Descending order.

sort_by_city_and_grade_descending = df.sort_values(['City', 'Grade'], ascending=False)

       Name  Age       City  Gender Grade
2      Joel   29  Hyderabad    Male     C
1     Sailu   35  Hyderabad  Female     B
5       Raj   34    Chennai    Male     C
3     Chamu   35    Chennai  Female     A
4  Jitendra   52  Bangalore    Male     B
0   Krishna   34  Bangalore    Male     A

 

Example 3: Sort by City in ascending order and Grade in Descending order.

We can set the sort order of columns by passing an array of Booleans to the ascending parameter.

sort_by_city_ascending_and_grade_descending = df.sort_values(['City', 'Grade'], ascending=[True, False])

       Name  Age       City  Gender Grade
4  Jitendra   52  Bangalore    Male     B
0   Krishna   34  Bangalore    Male     A
5       Raj   34    Chennai    Male     C
3     Chamu   35    Chennai  Female     A
2      Joel   29  Hyderabad    Male     C
1     Sailu   35  Hyderabad  Female     B

 

Find the below working application.

 

sort_by_two_columns.py
import pandas as pd

# Create a sample DataFrame
data = {'Name': ['Krishna', 'Sailu', 'Joel', 'Chamu', 'Jitendra', "Raj"],
        'Age': [34, 35, 29, 35, 52, 34],
        'City': ['Bangalore', 'Hyderabad', 'Hyderabad', 'Chennai', 'Bangalore', 'Chennai'],
        'Gender': ['Male', 'Female', 'Male', 'Female', 'Male', 'Male'],
        'Grade': ['A', 'B', 'C', 'A', 'B', 'C']}
df = pd.DataFrame(data)
print(df)

# Sort by City and Grade Ascending
print('\nSorty by City and Grade Ascending')
sort_by_city_and_grade_ascending = df.sort_values(['City', 'Grade'])
print(sort_by_city_and_grade_ascending)

# Sort by City and Grade Descending
print('\nSorty by City and Grade Descending')
sort_by_city_and_grade_descending = df.sort_values(['City', 'Grade'], ascending=False)
print(sort_by_city_and_grade_descending)

# Sort by City in ascending and Grade Descending
print('\nSort by City in ascending and Grade Descending')
sort_by_city_ascending_and_grade_descending = df.sort_values(['City', 'Grade'], ascending=[True, False])
print(sort_by_city_ascending_and_grade_descending)

Output

       Name  Age       City  Gender Grade
0   Krishna   34  Bangalore    Male     A
1     Sailu   35  Hyderabad  Female     B
2      Joel   29  Hyderabad    Male     C
3     Chamu   35    Chennai  Female     A
4  Jitendra   52  Bangalore    Male     B
5       Raj   34    Chennai    Male     C

Sorty by City and Grade Ascending
       Name  Age       City  Gender Grade
0   Krishna   34  Bangalore    Male     A
4  Jitendra   52  Bangalore    Male     B
3     Chamu   35    Chennai  Female     A
5       Raj   34    Chennai    Male     C
1     Sailu   35  Hyderabad  Female     B
2      Joel   29  Hyderabad    Male     C

Sorty by City and Grade Descending
       Name  Age       City  Gender Grade
2      Joel   29  Hyderabad    Male     C
1     Sailu   35  Hyderabad  Female     B
5       Raj   34    Chennai    Male     C
3     Chamu   35    Chennai  Female     A
4  Jitendra   52  Bangalore    Male     B
0   Krishna   34  Bangalore    Male     A

Sort by City in ascending and Grade Descending
       Name  Age       City  Gender Grade
4  Jitendra   52  Bangalore    Male     B
0   Krishna   34  Bangalore    Male     A
5       Raj   34    Chennai    Male     C
3     Chamu   35    Chennai  Female     A
2      Joel   29  Hyderabad    Male     C
1     Sailu   35  Hyderabad  Female     B


Previous                                                 Next                                                 Home

No comments:

Post a Comment