Search code examples
pythonpandaspandas-groupby

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


Solution

  • 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)