0

I am trying to use groupby to find and return a subset of df rows.

df = pd.DataFrame({'id': ['123', '123', '134', 134], 'Date': [2023-02-01, 2023-02-04, 2023-02-02, 2023-02-04], 'title' : ['first', 'first', 'second', 'second'})

I want to return the most recent by id.

I tried df.groupby('id')['Date'].max()

that returns a series.

also df.loc[df.groupby("id")["Date"].idxmax()] I get a value Error

I want to return:

id Date title
1 123 2023-02-04 first
3 134 2023-02-04 second

0 Answers0