时间序列数据帧。用相对起始点绘制两个不同的时间范围

2024-10-01 00:29:36 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个带有时间序列的数据框(单只股票的每日价格)。我想取两个不同的时间范围,并将它们覆盖在相对起点为0而不是日期的绘图上

在下面的示例中,如果我绘制1962年和2018年,它使用日期作为x轴,而不是相对起点

SPY = pd.read_csv('GSPC.csv', parse_dates=['dDate'], index_col='dDate')

SPY1962 = SPY['1962']
SPY2018 = SPY['2018']

firstprice62 = SPY1962['nAdjClose'].iloc[0]
firstprice18 = SPY2018['nAdjClose'].iloc[0]

normal62 = SPY1962['nAdjClose'].div(firstprice62).mul(100)
normal18 = SPY2018['nAdjClose'].div(firstprice18).mul(100)

A picture of what I'm trying to accomplish


Tags: csv数据div时间序列起点mulspy
1条回答
网友
1楼 · 发布于 2024-10-01 00:29:36

我明白了

normal18 = normal18.reset_index()
normal62 = normal62.reset_index()

normal62['nAdjClose'].plot()
normal18['nAdjClose'].plot()

plt.show()

相关问题 更多 >