How to Sort a Pandas DataFrame by Date

python_tutorials

Introduction

Pandas is an extremely popular data manipulation and analysis library. It’s the go-to tool for loading in and analyzing datasets for many.

Correctly sorting data is a crucial element of many tasks regarding data analysis. In this tutorial, we’ll take a look at how to sort a Pandas DataFrame by date.

Let’s start off with making a simple DataFrame with a few dates:

import pandas as pd

data = {'Name':["John", "Paul", "Dhilan", "Bob", "Henry"], 
'Date of Birth': ["01/06/86", "05/10/77", "11/12/88", "25/12/82", "01/06/86"]}
df = pd.DataFrame(data) 

print(df)

By default our output is sorted by the DataFrames index:

    Name Date of Birth
0    John      01/06/86
1    Paul      05/10/77
2  Dhilan      11/12/88
3     Bob      25/12/82
4   Henry      01/06/86

The eagle-eyed may notice that John and Paul have the same date of birth – this is on-purpose as we’ll see in a moment.

Convert Strings

 

 

To finish reading, please visit source site