MNF
MNF

Reputation: 1

Changing the Pandas DataFrame structure in Python

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.

Upvotes: 0

Views: 27

Answers (1)

Erfan
Erfan

Reputation: 42916

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

Upvotes: 1

Related Questions