Pandas: Subtract dates into grouped indexes in a dataframe

Asked

Viewed 348 times

2

I am conducting a research using pandas and need to infer the time between two buses based on their start times (start_time). For this I have grouped in my dataframe a field for itinerary and Leg(otp_itinerary_id and otp_leg_id,respectively). And the bus schedules are always on the even Legs.

So I wanted help to perform a subtraction of the bt_start_time of even Legs.

Follow the image for more details. If anyone can help me, I’d really appreciate it.

inserir a descrição da imagem aqui

1 answer

1


Friend, it can be done by going through the table with the method itertuples() as follows:

import pandas as pd

df = pd.read_excel('suaPlanilha.xlsx')

for row in df.itertuples():
    if row.leg % 2 == 0:
        print(row.start_time2 - row.start_time1 )
  • thanks @Murilo-portugal I managed to apply the itertuples to the context of my problem and managed to solve it.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.