Home > Enterprise >  how to add the value from the next row to the current row
how to add the value from the next row to the current row

Time:04-10

I want to groupby 'id' column and add the value from the next row to the current row only for 'trip' column

How can I transform the first dataframe to second dataframe show below?

enter image description here

enter image description here

CodePudding user response:

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