How to Sort Pandas DataFrame by One Column's Values

  1. Dataframe Sorting Order - Argument ascending
  2. dataframe Sorting Order - Argument na_position

We will introduce pandas.DataFrame.sort_values method to sort the dataframe values, and its options like ascending to specify the sorting order and na_position that determines the position of NaN in the sorted result.

Consider the following dataframe,

import pandas as pd
df = pd.DataFrame({
    'col1': ['g', 't', 'n', 'w', 'n', 'g'],
    'col2': [5, 2, 5, 1, 3, 6],
    'col3': [0, 7, 2, 8,1, 2],
})
print(df)

If you run this code you will get the output as following which is not sorted yet.

    col1  col2  col3
0    g     5     0
1    t     2     7
2    n     5     2
3    w     1     8
4    n     3     1
5    g     6     2

Now we could sort the dataframe with the codes below.

import pandas as pd
df = pd.DataFrame({
    'col1': ['g', 't', 'n', 'w', 'n', 'g'],
    'col2': [5, 2, 5, 1, 3, 6],
    'col3': [0, 7, 2, 8,1, 2],
})
print(df.sort_values(by=['col1']))

We sort dataframe by col1. After running the above code you will get the following output.

    col1  col2  col3
0    g     5     0
5    g     6     2
2    n     5     2
4    n     3     1
1    t     2     7
3    w     1     8

We can use more than one column for sorting as well. Let’s change the last line of the above codes as follow,

print(df.sort_values(by=['col1','col2']))

After running the code we will get the following output.

    col1  col2  col3
0    g     5     0
5    g     6     2
4    n     3     1
2    n     5     2
1    t     2     7
3    w     1     8

Now dataframe is further sorted by col2 as well.

Dataframe Sorting Order - Argument ascending

By default sorting is in ascending order, to sort dataframe in descending order, we need to set flag ascending=false.

print(df.sort_values(by=['col1','col2'], ascending=False))

After running the code we will get the following output.

    col1  col2  col3
3    w     1     8
1    t     2     7
2    n     5     2
4    n     3     1
5    g     6     2
0    g     5     0

dataframe Sorting Order - Argument na_position

na_position specifies the position of NaN after sorting i.e. last puts NaN at the end. Its default value is first that puts NaN at the beginning of the sorted result.

Consider the following dataframe,

import numpy as np
import pandas as pd
s = pd.Series([np.nan, 2, 4, 10, 7])
print(s.sort_values(na_position='last'))

After running the code we will get the following output.

1     2.0
2     4.0
4     7.0
3    10.0
0     NaN

Related Article - Pandas DataFrame

  • How to Get Pandas DataFrame Column Headers as a List
  • How to Delete Pandas DataFrame Column
  • How to Convert DataFrame Column to Datetime in Pandas
  • How to Convert a float to an integer in Pandas DataFrame
  • How to get the aggregate of Pandas group-by and Sum
  • How to convert Python dictionary to Pandas DataFrame
  • How to add header row to a Pandas DataFrame
  • How to convert Pandas Dataframe to Numpy array
  • How to convert index of a Pandas Dataframe into a column
  • How to count the NaN occurrences in a column in Pandas Dataframe
  • How to change the order of Pandas DataFrame columns
  • How to add one row to Pandas DataFrame
  • How to delete a row based on column value in Pandas DataFrame
  • How to get a value from a cell of a Pandas DataFrame
  • How to Convert DataFrame Column to String in Pandas
  • How to rename columns in Pandas DataFrame
  • How to set value for particular cell in pandas DataFrame using index
  • How to add a new column to existing DataFrame with default value in Pandas
  • How to filter dataframe rows based on column values in Pandas
  • How to create an empty column in Pandas DataFrame
  • How to iterate through rows of a DataFrame in Pandas
  • How to get index of all rows whose particular column satisfies given condition in Pandas
  • comments powered by Disqus