Reputation: 5676
I have a pandas DataFrame looking like this:
date info
A x
A y
B z
B x
C y
I only want to know the last date. In this case it is C.
I thought that I can get this by grouping and sorting by the Date
column:
df.groupby('date', sort=True)
... and then accessing the last group. However, there is no way of accessing the last group as one-liner? Is there a better way to do this?
Upvotes: 2
Views: 618
Reputation: 117370
If you just want to get the date as a value, your own answer is fine. But if you want to get the actual record with biggest date, you can use head()
:
In [4]: df.sort('date', ascending=False).head(1)
Out[4]:
date info
4 C y
You can also use ascending
parameter to sort by other columns.
In [4]: df.sort(columns=['date', 'info'], ascending=[False, True]).head(1)
Out[4]:
date info
4 C y
Upvotes: 1
Reputation: 5676
I think I was just over-complicating things. to get C this should be enough:
df['date'].max()
Upvotes: 2