数据框的所选部分的乘法

问题描述 投票:0回答:1

我目前正在清理由不同价格时间序列的股票组成的财务数据集。某些时间序列包含所谓的“股票分割”,这意味着股票数量已更改。举个例子:以下股票的数量在2017年已减半]

enter image description here

为了解决这个问题,我想将拆分事件之前的整个时间序列乘以0.5。为此,我做了以下工作。 df是具有不同股票的数据框,“ A”只是一个时间序列。

# searching for the stock split
df_return = df["A"].pct_change(1)                       # calculate returns
df_loc = df_return["A"].index.get_loc(df_return["A"].idxmin())   # get "location" of stock split

下一步,我需要将df中0:df_loc的所有值乘以0.5。我失败了,例如:

df = df.replace([df["A"].iloc[:df_loc]], df*2)

有人知道如何解决此问题吗?我认为这应该很容易,但是我无法弄清楚。在此先感谢

python pandas time-series finance
1个回答
1
投票

您可能想要这样做。

df.loc[:df_loc, 'price'] *= 0.5
© www.soinside.com 2019 - 2024. All rights reserved.