Wei Hong
Wei Hong

Reputation: 57

Sorting data with a column of data need to be grouped in python

I got a database as below:

import pandas as pd
df = pd.read_excel(data.xlsx)
print(df)

The database looks like this:

|ID|Date  |Name|
|1 |Jan 19|B   |
|2 |Mar 19|A   |
|3 |Apr 19|C   |
|4 |Apr 19|B   |
|5 |May 19|C   |
|6 |Jun 19|A   |

So I wanted to sort in a way that "Name" will be grouped together and sorted based on the date rather than the alphabet and preserve the ID as below:

|ID|Date  |Name|
|1 |Jan 19|B   |
|4 |Apr 19|B   |
|2 |Mar 19|A   |
|6 |May 19|A   |
|3 |Apr 19|C   |
|5 |May 19|C   |

Thanks for any advice in advance.

Upvotes: 2

Views: 40

Answers (1)

jezrael
jezrael

Reputation: 862681

Convert Date column to datetimes and Name to ordered Categiricals for correct order in output DataFrame:

df['Date'] = pd.to_datetime(df['Date'], format='%b %y')

df['Name'] = pd.Categorical(df['Name'], ordered=True, categories=df['Name'].unique())

df = df.sort_values(['Name','Date'])
print (df)
   ID       Date Name
0   1 2019-01-01    B
3   4 2019-04-01    B
1   2 2019-03-01    A
5   6 2019-06-01    A
2   3 2019-04-01    C
4   5 2019-05-01    C

Upvotes: 3

Related Questions