Skip to content
Advertisement

Python pandas group by check if value changed then previous value

I’ve a problem with groupby function of pandas’s library. I’ve the following dataframe.

JavaScript
id result date
400001 N 2020-07-03
400001 N 2021-09-09
400001 P 2021-10-27
400002 N 2020-07-03
400003 N 2020-06-30
400003 N 2022-04-27
400004 P 2020-06-30
400004 N 2022-04-27

I need to group by column ‘id’ and extract the value of column ‘date’ where the value of column ‘result’ change. If value in column ‘result’ doesn’t change, keep the first value of column ‘date’.

This an example:

id date
400001 2021-10-27
400002 2020-07-03
400003 2020-06-30
400004 2022-04-27

I’ve tried this:

JavaScript

but the function doesn’t works so well. The function verify the difference versus first element of column ‘id’ of groupby selection. I don’t need this.

Can you help me? Thanks

Advertisement

Answer

You can compute a cumsum of the booleans identifying the changes. Then get the max index:

JavaScript

Output:

JavaScript

NB. The input provided as DataFrame is different from the one as table. The output matching the DataFrame is shown here.

If needed, sort the dates first:

JavaScript

Output:

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