0

I want to groupby multiple column based on a particular column in python. When doing so, for a particular column I want the value to last but one row'th value. If I wanted the same for all column, I would've have used nth method. but I want it for a particular column and I couldn't find the solution online. How can I do this?

Aggregated_Halt = df.groupby('Group_Halt').agg({'Date_Time': [**last but one row**], 'Latitude': ['last'],'Longitude': ['last'], 'Time_diff':'sum', 'LocationAddress': 'last'})

Here I want datetime column to have last but one row value.

1
  • Would be better if you could share a sample of input dataframe with expected output. Commented Nov 20, 2020 at 7:12

1 Answer 1

1

Try this:

Aggregated_Halt = df.groupby('Group_Halt').agg({'Date_Time': lambda x: x[-2], 'Latitude': ['last'],'Longitude': ['last'], 'Time_diff':'sum', 'LocationAddress': 'last'})
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.