Sorting by Multiple Columns as per date

We can further extend our understanding for sorting multiple datetime columns as well, in this, we maintain a priority order to sort our DataFrame. Let’s have a look.

 

Step 1: Load or create dataframe having multiple date columns 

Python




# importing package
import pandas as pd
 
# Creating a DataFrame
data_1 = pd.DataFrame({'Mfg. Date': ['2021-01-25','2021-01-22','2021-01-20','2021-01-18',
                       '2021-01-22','2021-01-17','2021-01-21'],
                       'ProductID': [7,5,3,2,6,1,4],
                       'Product Name': ['Paracetamol','Moov','Volini','Crocin',
                                        'Aciloc','Iodex','Combiflam'],
                       'Expiry Date':['2022-01-25','2023-01-22','2021-05-20','2022-03-18',
                                      '2022-01-22','2021-05-17','2022-01-30']
                       })
 
# Checking dataframe
print(data_1)


 
Output:

 

Here, it can be clearly seen that our DataFrame contains two columns having dates namely ‘Mfg. Date’ and ‘Expiry Date ‘.

Step 2: Converting string data type into datetime object.

Python




data_1[['Mfg. Date','Expiry Date']] = data_1[['Mfg. Date','Expiry Date']].apply(pd.to_datetime)


 
 Step 3: Sorting the DataFrame as per date 

Python




# sorting DataFrame by giving first priority to Expiry Date and then Mfg. Date
data_1.sort_values(by=['Expiry Date', 'Mfg. Date'])


 
 Output:

 Here, we got a sorted list in ascending order of Expiry Date as per our priority.

 



How to Sort a Pandas DataFrame by Date?

In the real world, we can come across datasets of any form that may include the date inside them too. These datasets can be present in any file format like .CSV, .xlsx, .txt, etc. To load this data inside Python, we use a library named Pandas which provides us a plethora of functions and methods to play around with this data. Pandas read these datasets in the form of DataFrames.

Sometimes, there might be a situation where the dataset may contain attributes related to date, and we want to sort the records inside the dataframe as per the date values in a specific order.

In this article, we will learn about how can we sort Pandas DataFrame by the Date. I’ll be creating a custom dataframe object imitating a real-world problem and this method will work universally for any DataFrame.

Similar Reads

Sorting by Single Column

To sort a DataFrame as per the column containing date we’ll be following a series of steps, so let’s learn along....

Sorting by Multiple Columns as per date

...