I want to calculate the distance that my cars have driven. I have all the coordinates that the cars need to go to. Some cars parks earlier then others, and this messes up my calculation. I have this:
cars= pd.DataFrame({'x': [3,3,3,3,3,3,3,3,3],
'y': [1,2,3,4,5,6,7,8,9],
'x_goal_1': [3,3,3,3,3,3,3,3,3],
'y_goal_1': [10,10,10,10,10,10,10,10,10],
'x_goal_2': [17,24,31,31,17,17,38,38,31],
'y_goal_2': [10,10,10,10,10,10,10,10,10],
'x_goal_3': [17,24,31,31,17,17,38,38,31],
'y_goal_3': [17, 3, 3, 3, 17, 17, 17, 17, 3],
'x_goal_4': [None,27,35,28,14,18,42,43,None],
'y_goal_4': [None, 3, 3, 3, 17, 17, 17, 17, None],
'z': [3,4,5,6,7,8,9,12,22]})
cars['moved_tot'] = (
abs(cars['x']-cars['x_goal_1']) + abs(cars['y']-cars['y_goal_1']) +
abs(cars['x_goal_1']-cars['x_goal_2']) + abs(cars['y_goal_1']-cars['y_goal_2']) +
abs(cars['x_goal_2']-cars['x_goal_3']) + abs(cars['y_goal_2']-cars['y_goal_3']) +
abs(cars['x_goal_3']-cars['x_goal_4']) + abs(cars['y_goal_3']-cars['y_goal_4']) )
I then get:
x y x_goal_1 y_goal_1 ... x_goal_4 y_goal_4 z moved_tot
0 3 1 3 10 ... NaN NaN 3 NaN
1 3 2 3 10 ... 27.0 3.0 4 39.0
2 3 3 3 10 ... 35.0 3.0 5 46.0
3 3 4 3 10 ... 28.0 3.0 6 44.0
4 3 5 3 10 ... 14.0 17.0 7 29.0
5 3 6 3 10 ... 18.0 17.0 8 26.0
6 3 7 3 10 ... 42.0 17.0 9 49.0
7 3 8 3 10 ... 43.0 17.0 12 49.0
8 3 9 3 10 ... NaN NaN 22 NaN
I want the first moved_tot I want 30, and in the last I want 36. I want the calculation to ignore if a value is None ( that is if this car has parked earlier ). How do I do this?
with help from David S ( thank you! ) I figured out how to do it.
bags['moved_tot'] = (
abs(bags['x']-bags['x_goal_1']).fillna(0) + abs(bags['y']-bags['y_goal_1']).fillna(0) +
abs(bags['x_goal_1']-bags['x_goal_2']).fillna(0) + abs(bags['y_goal_1']-bags['y_goal_2']).fillna(0) +
abs(bags['x_goal_2']-bags['x_goal_3']).fillna(0) + abs(bags['y_goal_2']-bags['y_goal_3']).fillna(0) +
abs(bags['x_goal_3']-bags['x_goal_4']).fillna(0) + abs(bags['y_goal_3']-bags['y_goal_4']).fillna(0)
)