In python, how to I get the minimum date in a group conditional on another value.
E.g. So below I want the minimum date by id, where value == 1 or if there are no values equal to 1 (value == 1) then the minimum date where value == 0.
|----|--------------|-------------|
| id | date | value |
|----|--------------|-------------|
| 1 | 2020-01-01 | 1 |
| 1 | 2020-01-04 | 1 |
| 1 | 2020-01-05 | 1 |
| 2 | 2020-01-01 | 1 |
| 3 | 2020-01-01 | 0 |
| 3 | 2020-01-05 | 1 |
| 4 | 2020-01-05 | 0 |
|----|--------------|-------------|
Expect:
|----|--------------|-------------|
| id | date | value |
|----|--------------|-------------|
| 1 | 2020-01-01 | 1 |
| 2 | 2020-01-01 | 1 |
| 3 | 2020-01-05 | 1 |
| 4 | 2020-01-05 | 0 |
|----|--------------|-------------|
Use
DataFrame.sort_values
by all 3 columns and then remove duplicates byid
column withDataFrame.drop_duplicates
: