Python_让特征值滞后一行
# 加载库
import pandas as pd
# 데이터프레임을 만듭니다.
dataframe = pd.DataFrame()
# 模拟数据
dataframe["dates"] = pd.date_range("1/1/2001", periods=5, freq="D")
dataframe["stock_price"] = [1.1,2.2,3.3,4.4,5.5]
dataframe.head()
# 让值滞后一行
dataframe["previous_days_stock_price"] = dataframe["stock_price"].shift(1)
dataframe.head()
dates stock_price previous_days_stock_price
0 2001-01-01 1.1 NaN
1 2001-01-02 2.2 1.1
2 2001-01-03 3.3 2.2
3 2001-01-04 4.4 3.3
4 2001-01-05 5.5 4.4