I have a pandas dataframe with dates on each row, I would like to groupby a second column and sort the dates then assign letter A to the first date and B to the second etc. See example below where I am stuck.
import pandas as pd
rng = pd.date_range('2015-02-24', periods=5, freq='T')
names = ['MAK', 'MAK', 'OKA', 'OKA', 'MAK']
df = pd.DataFrame({ 'Date': rng, 'Groups': names })
df
Date Groups
0 2015-02-24 00:00:00 MAK
1 2015-02-24 00:01:00 MAK
2 2015-02-24 00:02:00 OKA
3 2015-02-24 00:03:00 OKA
4 2015-02-24 00:04:00 MAK
The result I would like is:
Date Groups Letter
0 2015-02-24 00:00:00 MAK A
1 2015-02-24 00:01:00 MAK B
2 2015-02-24 00:02:00 OKA A
3 2015-02-24 00:03:00 OKA B
4 2015-02-24 00:04:00 MAK C
Thinking I could define a function as such:
def assignLetters(row):
return sort == 'A'
df['Letter'] = df.groupby('Groups').Date.apply(assignLetters)
Any help would be appreciated! Maybe direct me in the right direction, not sure how to assign letters chronologically?