Skip to content
Advertisement

Group by in pandas for criteria on one column and getting records for other columns as-is

So my dataframe looks something like this –

JavaScript

I want to group by ORD_ID. And grab the record which is last in TIME for that ORD_ID (without performing any aggregate function on other columns). i.e. the desired output is –

JavaScript

How can this be achieved? (so only the last record in TIME as per each unique ORD_ID )

Advertisement

Answer

You don’t need groupby, drop_duplicates would do:

JavaScript

Or if you really want groupby:

JavaScript

Output:

JavaScript
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement