anat
anat

Reputation: 143

get latest string value of groupBy in python

I want to get the latest (by 'date') value of a key from a df in python. this is my data frame:

key     date      value
1    10/02/2010    'a'
1    13/02/2010    'c' 
1    10/02/2010    'a'   
1    11/02/2010    'b' 
3    15/12/2013    'r'   
3    04/03/2011    'g'   
3    04/03/2012    'a'   
4    11/12/2018    'b'  

sample output:

key     date      value
1    13/02/2010    'c'   
3    15/12/2013    'r'   
4    11/12/2018    'b' 

*****I know I can sort the data frame by dates and get the first row but I prefers not to use sort.

what can I do?

Upvotes: 0

Views: 55

Answers (2)

yatu
yatu

Reputation: 88236

This assumes key is the index, as it seems:

df = df.reset_index()
df.groupby('key').date.apply(lambda x: x.max())

Upvotes: 1

U13-Forward
U13-Forward

Reputation: 71580

Use the below, it does by 'date' as you wanted, and gets in expected order, and outputs expected result:

print(df[df.groupby('key')['date'].transform(max) == df['date']])

Upvotes: 2

Related Questions