Ace Sok :
+----+----------------+----------+---------------------+
| id | characteristic | location | value |
+----+----------------+----------+---------------------+
| 1 | start | loc1 | 01/01/2020 00:00:00 |
+----+----------------+----------+---------------------+
| 1 | end | loc1 | 01/01/2020 00:00:20 |
+----+----------------+----------+---------------------+
| 1 | start | loc2 | 01/01/2020 00:00:20 |
+----+----------------+----------+---------------------+
| 1 | end | loc2 | 01/01/2020 00:00:40 |
+----+----------------+----------+---------------------+
| 2 | start | loc1 | 01/01/2020 00:00:40 |
+----+----------------+----------+---------------------+
| 2 | end | loc1 | 01/01/2020 00:01:00 |
+----+----------------+----------+---------------------+
I have the above table and I would like to convert it to something like below
+----+---------------------+---------------------+----------+
| id | start | end | location |
+----+---------------------+---------------------+----------+
| 1 | 01/01/2020 00:00:00 | 01/01/2020 00:00:20 | loc1 |
+----+---------------------+---------------------+----------+
| 1 | 01/01/2020 00:00:20 | 01/01/2020 00:00:40 | loc2 |
+----+---------------------+---------------------+----------+
| 2 | 01/01/2020 00:00:40 | 01/01/2020 00:01:00 | loc1 |
+----+---------------------+---------------------+----------+
Please advise on how would you solve this. Thank you!!!
YOBEN_S :
We need use cumcount
create the help key , then this should be pivot problem
df['helpkey']=df.groupby(['id','characteristic']).cumcount()
s=df.set_index(['id','location','helpkey','characteristic'])['value'].unstack(level=3).reset_index().drop('helpkey',1)
s
characteristic id location end start
0 1 loc1 01/01/2020 00:00:20 01/01/2020 00:00:00
1 1 loc2 01/01/2020 00:00:40 01/01/2020 00:00:20
2 2 loc1 01/01/2020 00:01:00 01/01/2020 00:00:40