【问题标题】:how can i create these columns in python [duplicate]我如何在python中创建这些列[重复]
【发布时间】:2026-01-11 02:55:01
【问题描述】:

我有一个如下所示的数据集

Date           Price
2017-01-01     100
2017-01-02     187
2017-01-03     183

如何创建一个获取前几天信息的列

Date           Price    Previous_Days_Price
2017-01-01     100      NaN
2017-01-02     187      100
2017-01-03     183      187

【问题讨论】:

  • 是否可能缺少某些日期,例如2017-01-012017-01-032017-01-042017-01-062017-01-07

标签: python pandas dataframe


【解决方案1】:

pandas.Series.shift 是你想要的...

df['Price'].shift(1)

【讨论】: