How to Sort Data Frame

Here’s the tutorial of how to sort dataframe in Pandas.

This is the code which contains my data frame I’d like to sort.

import pandas as pd
import numpy as np

my_data = {'Column1': ['1', '4', '3', '4'],
     'Column2': ['5', '4', '2', '2'],
     'Column3': ['33', 'np.nan', '43', '2']}

my_df = pd.DataFrame(my_data)

print(f"This is my Data Frame: \n{my_df}")

how to sort data frame

How to sort data frame in ascending order

The easiest way to sort data frame is to sort by values in ascending order. To sort data frame by values use sort_values Pandas function and just put the name of the column as an argument.

import pandas as pd
import numpy as np

my_data = {'Column1': ['1', '4', '3', '4'],
     'Column2': ['5', '4', '2', '2'],
     'Column3': ['33', 'np.nan', '43', '2']}

my_df = pd.DataFrame(my_data)

print(f"This is my Data Frame: \n{my_df.sort_values('Column1')}")

Values of Column1 are sorted in ascending orrder.

How to sort data frame in descending order

Ascending is a default order. To sort data in descending order you need to extend parameters.
The first thing is to put by parameter to clearly state the column you need to get sorted. Next, add ascending parameter and set it as False.

import pandas as pd
import numpy as np

my_data = {'Column1': ['1', '4', '3', '4'],
     'Column2': ['5', '4', '2', '2'],
     'Column3': ['33', 'np.nan', '43', '2']}

my_df = pd.DataFrame(my_data)

print(f"This is my Data Frame: \n{my_df.sort_values(by='Column1', ascending=False)}")

The default value for the ascending parameter is True, so when sorting in descending order you need to use it and change the default value to False.

How to sort data frame by multiple columns

You know how to sort single column in Pandas. To sort multiple columns put columns names in brackets.

import pandas as pd
import numpy as np

my_data = {'Column1': ['1', '4', '3', '4'],
     'Column2': ['5', '4', '2', '2'],
     'Column3': ['33', 'np.nan', '43', '2']}

my_df = pd.DataFrame(my_data)

print(f"This is my Data Frame: \n{my_df.sort_values(by=['Column1','Column2'])}")

Pandas sorts values by multiple columns in the order how to typed.

How to sort data frame in both ascending and descending order

You may notice that columns contain the same values. You can sort them in any order you need. To sort columns different ways just add additional parameter in brackets and decide if you expect ascending (True) or descending (False) order.

import pandas as pd
import numpy as np

my_data = {'Column1': ['1', '4', '3', '4'],
     'Column2': ['5', '4', '2', '2'],
     'Column3': ['33', 'np.nan', '43', '2']}

my_df = pd.DataFrame(my_data)

print(f"This is my Data Frame: \n{my_df.sort_values(by=['Column1','Column2'],ascending=[False,True])}")

As you may notice, Pandas changed the order of values.

How to sort NaN values

Your data frame may contain NaN values. To sort NaN values in Pandas add an additional na_position parameter. You may choose if NaN values will be sorted as first or last.

import pandas as pd
import numpy as np

my_data = {'Column1': ['1', '4', '3', '4'],
     'Column2': ['5', '4', '2', '2'],
     'Column3': ['33', 'np.nan', '43', '2']}

my_df = pd.DataFrame(my_data)

print(f"This is my Data Frame: \n{my_df.sort_values(by='Column3',na_position='first')}")

The default value of the na_position parameter is ‘last’.

Sort_Values function documentation can be found here.

How to sort data frame in reverse order

To change the order of the entire data frame use the sort_index function. By changing ascending parameter to False you change the order of the whole data frame.

import pandas as pd
import numpy as np

my_data = {'Column1': ['1', '4', '3', '4'],
     'Column2': ['5', '4', '2', '2'],
     'Column3': ['33', 'np.nan', '43', '2']}

my_df = pd.DataFrame(my_data)

print(f"This is my Data Frame: \n{my_df.sort_index(ascending=False)}")

Sort_Index function documentation can be found here.

This Post Has 4 Comments

Leave a Reply