【发布时间】:2018-05-24 14:40:21
【问题描述】:
我有一个带有日期时间索引的数据框:
df = pd.DataFrame(
{'test':[1, 1, 1, 1, 1, 1]},
index=[
'2018-01-01', '2018-01-02', '2018-01-03',
'2019-01-03', '2019-01-02', '2020-01-02'
]
)
df.index= pd.to_datetime(df.index)
我有一个年度参数:
yearly_parameter = [1, 2, 3]
我想有效地(以矢量化方式?)将“test”列乘以它包含在列表 yearly_parameter 中的相应年度参数(第一个值是 2018 年,第二个是 2019 年,第三个是 2020 年)。我怎样才能有效地做到这一点?列表是存储这些年度参数以进行计算的好方法吗?
我希望列中出现以下结果,例如“答案”:
df['answer'] = [1, 1, 1, 2, 2, 3]
print(df)
test answer
2018-01-01 1 1
2018-01-02 1 1
2018-01-03 1 1
2019-01-03 1 2
2019-01-02 1 2
2020-01-02 1 3
非常感谢您的帮助,
皮埃尔
【问题讨论】:
标签: python performance pandas multiplication