How can I sort a Pandas DataFrame alphabetically?

How can I sort a Pandas DataFrame alphabetically?

To sort a Pandas DataFrame alphabetically, you can use the “sort_values()” function. This function allows you to specify the column(s) you want to sort by and the order in which you want them to be sorted. By default, the function sorts the data in ascending order. However, you can also specify “ascending=False” to sort the data in descending order. This method is useful for organizing data in a DataFrame based on alphabetical order, making it easier to identify patterns and relationships within the data.

Pandas: 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

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 .

Additional Resources

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

Cite this article

stats writer (2024). How can I sort a Pandas DataFrame alphabetically?. PSYCHOLOGICAL SCALES. Retrieved from https://scales.arabpsychology.com/stats/how-can-i-sort-a-pandas-dataframe-alphabetically/

stats writer. "How can I sort a Pandas DataFrame alphabetically?." PSYCHOLOGICAL SCALES, 29 Jun. 2024, https://scales.arabpsychology.com/stats/how-can-i-sort-a-pandas-dataframe-alphabetically/.

stats writer. "How can I sort a Pandas DataFrame alphabetically?." PSYCHOLOGICAL SCALES, 2024. https://scales.arabpsychology.com/stats/how-can-i-sort-a-pandas-dataframe-alphabetically/.

stats writer (2024) 'How can I sort a Pandas DataFrame alphabetically?', PSYCHOLOGICAL SCALES. Available at: https://scales.arabpsychology.com/stats/how-can-i-sort-a-pandas-dataframe-alphabetically/.

[1] stats writer, "How can I sort a Pandas DataFrame alphabetically?," PSYCHOLOGICAL SCALES, vol. X, no. Y, ص Z-Z, June, 2024.

stats writer. How can I sort a Pandas DataFrame alphabetically?. PSYCHOLOGICAL SCALES. 2024;vol(issue):pages.

Download Post (.PDF)
Slide Up
x
PDF
Scroll to Top