Home > Mobile >  Sort a pandas dataset by date
Sort a pandas dataset by date

Time:12-25

I have a CSV dataset like this.

import pandas as pd 
from io import StringIO


data="""
Date| Link
April 1, 2009, 12:00 PM| 4
March 27, 2009, 12:00 PM| 8
April 29, 2009, 12:00 PM| 15  
May 12, 2009, 12:00 PM| 9 
June 9, 2009, 12:00 PM| 11 
July 3, 2009, 12:00 PM| 329
June 16, 2009, 12:00 PM| 12
September 26, 2009, 12:00 PM| 48
October 4, 2009, 12:00 PM| 49
August 15, 2009, 12:00 PM| 10
November 30, 2009, 12:00 PM| 29
December 23, 2009, 12:00 PM| 68
April 1, 2009, 12:00 PM| 4  
May 12, 2010, 12:00 PM| 9 
September 26, 2012, 12:00 PM| 48 
"""

df = pd.read_csv(StringIO(data), delimiter='|')

Now I want to sort the dataset by the Date index; 1st day of calendar will need to appear first.

For this, I have tried using df.sort_values(by = 'Date') but unfortunately, it gives me the sorted index using alphabetical order. How can I sort this data set by as like in calendar?

CodePudding user response:

You can convert the Date column to datetime objects, and then use .sort_values:

df["Date"] = pd.to_datetime(df["Date"])
df.sort_values("Date")

This outputs:

                  Date   Link
1  2009-03-27 12:00:00      8
0  2009-04-01 12:00:00      4
12 2009-04-01 12:00:00      4
2  2009-04-29 12:00:00     15
3  2009-05-12 12:00:00      9
4  2009-06-09 12:00:00     11
6  2009-06-16 12:00:00     12
5  2009-07-03 12:00:00    329
9  2009-08-15 12:00:00     10
7  2009-09-26 12:00:00     48
8  2009-10-04 12:00:00     49
10 2009-11-30 12:00:00     29
11 2009-12-23 12:00:00     68
13 2010-05-12 12:00:00      9
14 2012-09-26 12:00:00     48
  • Related