Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I offset data using pandas

I have a data set that i needs column y data to be offset by n number of dates later. For example on the screenshot below, reference date for 22nd April should be offset by data in columns X 3 days later.

enter image description here

like image 703
Lee Hongwei Avatar asked Feb 12 '26 05:02

Lee Hongwei


1 Answers

If want shift each 3 days:

df.index = pd.to_datetime(df.index, format='%d-%b')
df['Y'] = df['X'].shift(-3, freq='d')

If want shift each 3 rows:

df['Y'] = df['X'].shift(-3)
like image 143
jezrael Avatar answered Feb 13 '26 19:02

jezrael



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!