0

My Pandas dataframe looks like this:

enter image description here

Notice that for each task there are exactly 2 rows: row for begin and and row for end of task.
I need to create another dataframe with columns Task, StartTime, EndTime where each row will represent task and it's lifetime.
There is an ugly way to do it (using iteration). Is there an elegant way?

IgorStack
  • 799
  • 2
  • 6
  • 22

2 Answers2

1

Using pandas.pivot() function you can simply do it like this assuming df is your dataframe:

new_df = df.pivot(index="Task", columns="Action", values="DateTime")
1

Another way is to use set_index and unstack:

>>> (df.set_index(['Task', 'Action'])['DateTime'].unstack()
       .reset_index().rename_axis(columns=None))

   Task                    begin                      end
0     1  2022-05-04 16:07:42.580  2022-05-04 16:07:53.565
Corralien
  • 109,409
  • 8
  • 28
  • 52