0
$\begingroup$

I have a DataFrame with duplicate id's but have different dates.

For Example:

A          B
1    12.09.2015
1    14.08.2018
1    13.07.2012
2    01.09.2015
2    09.10.2011

I want to group them by the Id's and get each of their maximum and minimum dates and create a different dataframe with each line for one id like:

A  B(min)  B(max)
$\endgroup$
2
$\begingroup$

All you need is a groupby operation + aggregation on the min/max values.

  • df.groupby('id').agg(('min','max'))['date_column']

The output should be like this : different dataframe with each line containing an id and the min/max dates.

output

| improve this answer | |
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.