Pandas: How to Sort DataFrame Alphabetically


You can use the following methods to sort the rows of a pandas DataFrame alphabetically:

Method 1: Sort by One Column Alphabetically

#sort A to Z
df.sort_values('column1')

#sort Z to A
df.sort_values('column1', ascending=False)

Method 2: Sort by Multiple Columns Alphabetically

#sort by column1 from Z to A, then by column2 from A to Z
df.sort_values(['column1', 'column2'], ascending=(False, True))

The following example shows how to use each method in practice.

Example 1: Sort by One Column Alphabetically

Suppose we have the following pandas DataFrame:

import pandas as pd

#create DataFrame
df = pd.DataFrame({'team': ['Mavs', 'Spurs', 'Lakers', 'Nuggets', 'Hawks'],
                   'points': [120, 108, 99, 104, 115]})

#view DataFrame
print(df)

      team  points
0     Mavs     120
1    Spurs     108
2   Lakers      99
3  Nuggets     104
4    Hawks     115

We can use the following syntax to sort the rows of the DataFrame by team name from A to Z:

#sort by team name A to Z
df_sorted = df.sort_values('team')

#view sorted DataFrame
print(df_sorted)

      team  points
4    Hawks     115
2   Lakers      99
0     Mavs     120
3  Nuggets     104
1    Spurs     108

Notice that the rows are now sorted by team name from A to Z.

We could also sort from Z to A:

#sort by team name Z to A
df_sorted = df.sort_values('team', ascending=False)

#view sorted DataFrame
print(df_sorted)

      team  points
1    Spurs     108
3  Nuggets     104
0     Mavs     120
2   Lakers      99
4    Hawks     115

And we could also use the reset_index() function to reset the index values in the sorted DataFrame:

#sort by team name A to Z and reset index
df_sorted = df.sort_values('team').reset_index(drop=True)

#view sorted DataFrame
print(df_sorted)

      team  points
0    Hawks     115
1   Lakers      99
2     Mavs     120
3  Nuggets     104
4    Spurs     108

Example 2: Sort by Multiple Columns Alphabetically

Suppose we have the following pandas DataFrame:

import pandas as pd

#create DataFrame
df = pd.DataFrame({'conference': ['West', 'West', 'West', 'East', 'East'],
                   'team': ['Mavs', 'Spurs', 'Lakers', 'Heat', 'Hawks'],
                   'points': [120, 108, 99, 104, 115]})

#view DataFrame
print(df)

  conference    team  points
0       West    Mavs     120
1       West   Spurs     108
2       West  Lakers      99
3       East    Heat     104
4       East   Hawks     115

We can use the following syntax to sort the rows of the DataFrame by conference name from A to Z, then by team name from Z to A:

#sort by conference name A to Z, then by team name Z to A
df_sorted = df.sort_values(['conference', 'team'], ascending=(True, False))

#view sorted DataFrame
print(df_sorted)

  conference    team  points
3       East    Heat     104
4       East   Hawks     115
1       West   Spurs     108
0       West    Mavs     120
2       West  Lakers      99

The rows are sorted by conference name from A to Z, then by team name from Z to A.

Note: You can find the complete documentation for the pandas sort_values() function here.

Additional Resources

The following tutorials explain how to perform other common operations in pandas:

Pandas: How to Sort by Date
Pandas: How to Sort Columns by Name
Pandas: How to Sort by Both Index and Column

Leave a Reply

Your email address will not be published.