I'm a python newbie - so please bear with me.
I am trying to create a set of shifted columns in a dataframe. For example I have a DataFrame df:
heading speed
time
2018-09-03 14:53:02 195.0 0.000000
2018-09-03 14:53:07 195.0 0.416667
2018-09-03 14:53:12 195.0 0.138889
2018-09-03 14:53:17 195.0 0.277778
2018-09-03 14:53:22 195.0 0.972222
2018-09-03 14:53:27 289.0 1.944444
I would like to end up with:
heading speed prev_heading prev_speed
time
2018-09-03 14:53:02 190.0 0.000000 NaN NaN
2018-09-03 14:53:07 195.0 0.416667 190.0 0.000000
2018-09-03 14:53:12 195.0 0.138889 195.0 0.416667
2018-09-03 14:53:17 195.0 0.277778 195.0 0.138889
2018-09-03 14:53:22 195.0 0.972222 195.0 0.277778
2018-09-03 14:53:27 289.0 1.944444 195.0 0.972222
I know how to do this with a single column:
df['prev_heading'] = df['heading'].shift(1)
But is there a way to do this with multiple columns at once say something like:
df['prev_heading', 'prev_speed'] = df['heading', 'speed'].shift(1)
It seems inefficient if I have to run the shift command multiple times, as I would like to parse the dataframe on once if I can.
Any help would be much appreciated.