I have a pandas dataframe with the values as follows:
Start end Depart Duration
A B 5 2
B C 5 3
C D 5 6
D E 5 4
I want the output as shown below. I need to add rows of 'Depart' and 'Duration' to form a new column 'Arrive' and pass the result value to the next row in Depart column and then add with Duration to form a value in Arrive and repeat the process again.
I also need to verify if the column values 'Start' is same as the 'End' value in previous row.
Start end Depart Arrive
A B 5 7
B C 7 10
C D 10 16
D E 16 20
I tried something like this:
if np.where(d.Start == d.End):
d['Arrive'] = d['Depart'] d['Duration']
d['Depart'] = d.Arrive.shift(1)
print(d)
The output is as follows. It still takes the original Depart value as 5 and adds up to give the result. Where am I going wrong?
Start End Depart Duration Arrive
0 A B NaN 2 7
1 B C 7.0 3 8
2 C D 8.0 6 11
3 D E 11.0 4 9
CodePudding user response:
Try using cumsum
:
depart = df['Depart'] df['Duration'].cumsum().shift(1).fillna(0).astype(int)
arrival = df['Depart'] df['Duration'].cumsum()
df['Depart'] = depart
df['Arrival'] = arrival
df = df.drop('Duration', axis=1)
Output:
>>> df
Start end Depart Arrival
0 A B 5 7
1 B C 7 10
2 C D 10 16
3 D E 16 20