Sorting a pandas DataFrame


This entry is part 6 of 9 in the series pandas DataFrame

You can sort a pandas DataFrame by one or multiple (one or more) columns using sort_values() method and by ascending or descending order. To specify the order, you have to use ascending boolean property; False for descending and True for ascending. By default, it is set to True. By default sort_values() returns a copy DataFrame with the result of the sort. To sort on current DataFrame use inplace=True.

Here below is a very small sample of data that we can practice on. Go ahead and copy it or use your own dataset.

import pandas as pd  # import the pandas library into Python
data = {'firstname': ['Bob', 'Sally', 'Suzie', 'Rohan'],
       'amount': [12, 67, 33, 41],
       'group': ['B', 'A', 'A', 'B']}
df = pd.DataFrame(data)
df

# sorts by group ascending (A, B, C etc.)
df2 = df.sort_values(['group'])
df2

Now I will sort on two columns. First I will sort on group, then amount. The order is important. First I sort on group, then within the group, I sort on amount. In this case both columns are sorted descending. If I had more columns, I could sort on three or more columns if I chose to. We just add to the list of column names and the list of True or False for ascending.

# the default ascending is True
df3 = df.sort_values(['group', 'amount'], ascending = [False, False])
df3

Series Navigation<< Reorder pandas DataFrame ColumnsPandas DataFrame Insert Column >>

Leave a comment

Your email address will not be published. Required fields are marked *