Quantcast
Channel: Active questions tagged python - Stack Overflow
Viewing all articles
Browse latest Browse all 13951

Update dataframe row given condition on duplicate

$
0
0

I have a dataframe like

    id      occurence    status0   1       1            validated1   2       1            validated2   3       1            validated3   1       10       

The association [id, occurence] is unique meaning I will only have 1 combination [1, 1] or [1, 10].The row with the occurence 10 will always be added on the dataframe after the one with the occurence 1.

What I want to do everytime I see new rows with occurence 10 is checking that another row with the same id and occurence 1 exists, then extract the status from the row [id, 1] and update the status of the row [id, 10] accordingly.

For now I can extract duplicated like this

df[df['id'].duplicated(keep=False)]

wich return a dataframe with all duplicates but I do not know how to do the part where I select the value of the status for the first occurence...

I also tried something like this:

df.groupby('id').transform(lambda x: x.update({'status': 'validated'}) with a static value at first but it gives me a dataframe filled with None...

Any ideau about this ?

Thanks


Viewing all articles
Browse latest Browse all 13951

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>