How to get min value by group conditional on another group in python?

125 views Asked by At

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        |
|----|--------------|-------------|
1

There are 1 answers

0
jezrael On BEST ANSWER

Use DataFrame.sort_values by all 3 columns and then remove duplicates by id column with DataFrame.drop_duplicates:

df['date'] = pd.to_datetime(df['date'])

df = (df.sort_values(['id','value','date'], ascending=[True, False, True])
        .drop_duplicates(['id']))
print (df)
   id       date  value
0   1 2020-01-01      1
3   2 2020-01-01      1
5   3 2020-01-05      1
6   4 2020-01-05      0