Skip to content
Advertisement

how to add the value from the next row to the current row

I want to group by id column and add the value from the next row to the current row only for the trip column How can I transform the first data frame to the second data frame shown below?

enter image description here

enter image description here

Advertisement

Answer

I am not sure if the only thing requested is to concatenate the trip ID of the next row to the current row but if it is, would you consider using shift(-1) ?

df['newtrip']=df['trip']+'-'+df['trip'].shift(-1)
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement