Skip to content
Advertisement

Pandas extract previous row on value change

I have two columns of data ‘Part & Qty’ where some part numbers are repeated across multiple rows. The quantity value I need to retrieve is contained in the final row before the part number changes.

My code (below) adds a True/False column to flag when the part number changes. I had thought when the flag is ‘True’ I want to retrieve the data from the previous row, however this does not work for the first and last rows.

Running my code gives the output (left) vs the data I’m trying to extract is(right):

output enter image description here

What is the best way to achieve this?

JavaScript

Advertisement

Answer

Try shift(-1):

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