0

I have the dataframe in Python that looks like below:

BatchID | EventTime

Test1   | 2020-03-10 10:22:21

Test1   | 2020-03-10 10:22:27

Test1   | 2020-03-10 10:22:31

Test2   | 2020-03-10 10:36:00

Test2   | 2020-03-10 10:36:02

Test2   | 2020-03-10 10:36:04

I want to restructure it such that it looks like as shown below:

BatchID --> Batch Start --> Batch End

Test1  --> 2020-03-10 10:22:21 --> 2020-03-10 10:22:31
Test2  --> 2020-03-10 10:36:00 --> 2020-03-10 10:36:04

Where for every unique Batch ID, I want to pick the start time and end time and frame it into one row.

1 Answer 1

1

Use groupby with agg and min, max:

df.groupby('BatchID')['EventTime'].agg(['min', 'max']).reset_index()
  BatchID                 min                 max
0   Test1 2020-03-10 10:22:21 2020-03-10 10:22:31
1   Test2 2020-03-10 10:36:00 2020-03-10 10:36:04
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.