note.nkmk.me

pandas: Sort DataFrame, Series with sort_values(), sort_index()

Posted: 2019-12-05 / Tags: Python, pandas

To sort pandas.DataFrame and pandas.Series, use sort_values() and sort_index(). You can sort in ascending / descending order, or sort by multiple columns.

Note that the sort() method in the old version is obsolete.

Here, the following contents will be described.

  • Sort by element (data): sort_values()
    • Ascending / Descending: ascending
    • Sort by multiple columns
    • Handle NaN: na_position
    • Change original object: inplace
    • Sort in row direction: axis
  • Sort by index / columns (row / column label): sort_index()
    • Sort by index (row label)
    • Ascending / Descending: ascending
    • Change original object: inplace
    • Sort by columns (column label): axis

The following data is used as an example.

import pandas as pd

df = pd.read_csv('data/src/sample_pandas_normal.csv')
print(df)
#       name  age state  point
# 0    Alice   24    NY     64
# 1      Bob   42    CA     92
# 2  Charlie   18    CA     70
# 3     Dave   68    TX     70
# 4    Ellen   24    CA     88
# 5    Frank   30    NY     57

Click here for sample CSV file.

The example uses pandas.DataFrame, but pandas.Series also provides reset_index(). The usage is the same.

Sponsored Link

Sort by element (data): sort_values()

To sort by element value, use the sort_values() method.

Specify the column label (column name) you want to sort in the first argument by.

df_s = df.sort_values('state')
print(df_s)
#       name  age state  point
# 1      Bob   42    CA     92
# 2  Charlie   18    CA     70
# 4    Ellen   24    CA     88
# 0    Alice   24    NY     64
# 5    Frank   30    NY     57
# 3     Dave   68    TX     70

Use reset_index() to reassign the index.

Ascending / Descending: ascending

The default is to sort in ascending order. If you need descending order, set the argument ascending to False.

df_s = df.sort_values('state', ascending=False)
print(df_s)
#       name  age state  point
# 3     Dave   68    TX     70
# 0    Alice   24    NY     64
# 5    Frank   30    NY     57
# 1      Bob   42    CA     92
# 2  Charlie   18    CA     70
# 4    Ellen   24    CA     88

Sort by multiple columns

If you specify the first argument by as a list, you can sort by multiple columns.

Sort from the back of the list in order. Finally, it sorts on the first specified column in the list.

df_s = df.sort_values(['state', 'age'])
print(df_s)
#       name  age state  point
# 2  Charlie   18    CA     70
# 4    Ellen   24    CA     88
# 1      Bob   42    CA     92
# 0    Alice   24    NY     64
# 5    Frank   30    NY     57
# 3     Dave   68    TX     70

df_s = df.sort_values(['age', 'state'])
print(df_s)
#       name  age state  point
# 2  Charlie   18    CA     70
# 4    Ellen   24    CA     88
# 0    Alice   24    NY     64
# 5    Frank   30    NY     57
# 1      Bob   42    CA     92
# 3     Dave   68    TX     70

If the argument ascending is specified in a list, ascending / descending order can be selected for each column.

df_s = df.sort_values(['age', 'state'], ascending=[True, False])
print(df_s)
#       name  age state  point
# 2  Charlie   18    CA     70
# 0    Alice   24    NY     64
# 4    Ellen   24    CA     88
# 5    Frank   30    NY     57
# 1      Bob   42    CA     92
# 3     Dave   68    TX     70

Handle NaN: na_position

If there is a missing value NaN, by default it is listed at the end.

df_nan = df.copy()
df_nan.iloc[:2, 1] = pd.np.nan
print(df_nan)
#       name   age state  point
# 0    Alice   NaN    NY     64
# 1      Bob   NaN    CA     92
# 2  Charlie  18.0    CA     70
# 3     Dave  68.0    TX     70
# 4    Ellen  24.0    CA     88
# 5    Frank  30.0    NY     57

df_nan_s = df_nan.sort_values('age')
print(df_nan_s)
#       name   age state  point
# 2  Charlie  18.0    CA     70
# 4    Ellen  24.0    CA     88
# 5    Frank  30.0    NY     57
# 3     Dave  68.0    TX     70
# 0    Alice   NaN    NY     64
# 1      Bob   NaN    CA     92

If the argument na_position='first', it will be listed at the top.

df_nan_s = df_nan.sort_values('age', na_position='first')
print(df_nan_s)
#       name   age state  point
# 0    Alice   NaN    NY     64
# 1      Bob   NaN    CA     92
# 2  Charlie  18.0    CA     70
# 4    Ellen  24.0    CA     88
# 5    Frank  30.0    NY     57
# 3     Dave  68.0    TX     70

Change original object: inplace

By default, new sorted objects are returned, but if the inplace argument is set to True, the original object itself is changed.

df.sort_values('state', inplace=True)
print(df)
#       name  age state  point
# 1      Bob   42    CA     92
# 2  Charlie   18    CA     70
# 4    Ellen   24    CA     88
# 0    Alice   24    NY     64
# 5    Frank   30    NY     57
# 3     Dave   68    TX     70

Sort in row direction: axis

As in the previous examples, by default, sorting is performed in the column direction (vertical direction).

If you want to sort in the row direction, set the argument axis to 1. Other arguments can be used in the same way as the previous examples.

Since an error occurs when a number and a string are mixed, in this example, string columns are deleted so that only numeric columns are used. See the following article for the drop() method.

df_d = df.drop(['name', 'state'], axis=1)
print(df_d)
#    age  point
# 1   42     92
# 2   18     70
# 4   24     88
# 0   24     64
# 5   30     57
# 3   68     70

df_d .sort_values(by=1, axis=1, ascending=False, inplace=True)
print(df_d)
#    point  age
# 1     92   42
# 2     70   18
# 4     88   24
# 0     64   24
# 5     57   30
# 3     70   68

Sort by index / columns (row / column label): sort_index()

To sort by index / columns (row / column name), use the sort_index() method.

Sort by index (row label)

By default, sort_index() sorts in the column direction (vertical direction) according to the row name (label).

print(df)
#       name  age state  point
# 1      Bob   42    CA     92
# 2  Charlie   18    CA     70
# 4    Ellen   24    CA     88
# 0    Alice   24    NY     64
# 5    Frank   30    NY     57
# 3     Dave   68    TX     70

df_s = df.sort_index()
print(df_s)
#       name  age state  point
# 0    Alice   24    NY     64
# 1      Bob   42    CA     92
# 2  Charlie   18    CA     70
# 3     Dave   68    TX     70
# 4    Ellen   24    CA     88
# 5    Frank   30    NY     57

Ascending / Descending: ascending

As with sort_values(), the default is to sort in ascending order. If you need descending order, set the argument ascending to False.

df_s = df.sort_index(ascending=False)
print(df_s)
#       name  age state  point
# 5    Frank   30    NY     57
# 4    Ellen   24    CA     88
# 3     Dave   68    TX     70
# 2  Charlie   18    CA     70
# 1      Bob   42    CA     92
# 0    Alice   24    NY     64

Change original object: inplace

As with sort_values(), you can use the argument inplace. Setting it to True changes the original object.

df.sort_index(inplace=True)
print(df)
#       name  age state  point
# 0    Alice   24    NY     64
# 1      Bob   42    CA     92
# 2  Charlie   18    CA     70
# 3     Dave   68    TX     70
# 4    Ellen   24    CA     88
# 5    Frank   30    NY     57

Sort by columns (column label): axis

By setting axis=1, it is sorted in the row direction (horizontal direction) according to the colmuns (column label). Other arguments can be used as in the previous examples.

df_s = df.sort_index(axis=1)
print(df_s)
#    age     name  point state
# 0   24    Alice     64    NY
# 1   42      Bob     92    CA
# 2   18  Charlie     70    CA
# 3   68     Dave     70    TX
# 4   24    Ellen     88    CA
# 5   30    Frank     57    NY

df.sort_index(axis=1, ascending=False, inplace=True)
print(df)
#   state  point     name  age
# 0    NY     64    Alice   24
# 1    CA     92      Bob   42
# 2    CA     70  Charlie   18
# 3    TX     70     Dave   68
# 4    CA     88    Ellen   24
# 5    NY     57    Frank   30
Sponsored Link
Share

Related Categories

Related Posts