I have a dataframe as follows:
id | time
1 | 10:21
1 | 10:22
1 | 10:23
2 | 10:40
2 | 10:45
2 | 10:50
I would like to add a new column as follows:
id | time | new_time
1 | 10:21 | 10:22
1 | 10:22 | 10:23
1 | 10:23 | None
2 | 10:40 | 10:45
2 | 10:45 | 10:50
2 | 10:50 | None
That means, I would like to create the new column by matching the values in the id
column. For example, if the id
values for two consecutive rows are the same then I would like to add the value in the new_time
(in the first column) from the time
value of the second column. If the id
values are not the same then I would like to add None
for the new_time
value.How can I achieve this using python or pandas?