简体   繁体   English

从熊猫数据帧(股票)计算数据的更有效方法

[英]More efficient way of calculating data from pandas dataframe (stock)

I was wondering if there is a more efficient/cleaner way of doing the following. 我想知道是否有更有效/更清洁的方法来进行以下操作。 Say I have a dataframe that contains 2 columns, the percentage, (base on previous price) and the action, play/buy (1) or not play/sell (-1). 假设我有一个数据框,其中包含2列,即百分比(基于先前价格)和操作,播放/购买(1)或不播放/出售(-1)。 Its basically about stocks. 它基本上是关于股票的。

For simplicity, consider the example df: 为简单起见,请考虑示例df:

Percent    Action
1.25       1
1.20       1
0.50       -1
0.75       1

I would like to generate the following. 我想产生以下内容。 I only care about the final money amount, I am just showing this table for reference. 我只在乎最终金额,我只显示此表以供参考。 Say we started with $100 and a state of not playing. 假设我们以$ 100开始,并且处于未玩状态。 Thus we should get the money amount of: 因此,我们应该得到以下金额:

Playing    Percent    Action    Money
No         1.25       1         $100
Yes        1.20       1         $120
Yes        0.50       -1        $60
No         0.75       1         $60
Yes        ...        ...       ...

The amount didnt change in the first row since we weren't playing yet. 由于我们还没有参加比赛,因此第一排的金额没有变化。 Since the action is 1, we will play the next one. 由于动作是1,我们将播放下一个。 The percentage went up 20%, thus we get $120. 百分比上升了20%,因此我们得到了120美元。 The next action is still a 1, so we'll still be in the next one. 下一个动作仍然是1,所以我们仍然会在下一个动作中。 The percentage went down to 50% so we end up with $60. 百分比下降到50%,因此我们最终得到了60美元。 Next action is -1, thus we will not play. 下一个动作是-1,因此我们将不玩。 The percentage went down to 75%, but since we weren't playing, our money stayed the same. 百分比下降到75%,但是由于我们不玩游戏,我们的钱保持不变。 And so on. 等等。

Currently, I have the code below. 目前,我有下面的代码。 It works fine, but just wondering if there is a more efficient way using numpy/pandas functions. 它工作正常,但只是想知道是否有使用numpy / pandas函数的更有效方法。 Mine basically iterates through each row and calculate the value. 我的基本上是遍历每一行并计算值。

playing = False
money = 10000

for index, row in df.iterrows():
   ## UPDATE MONEY IF PLAYING
   if index > 0 and playing == True:
      money = float(format(money*row['Percent'],'.2f'))

   ## BUY/SELL
   if row['Action'] == 1:
      if playing == False:
         playing = True         ## Buy, playing after this
      elif row['Action'] == -1:
         if playing == True:
            playing = False   ## Sell, not playing after this

You could try this: 您可以尝试以下方法:

# decide whether to play based on action
df['Playing'] = df.Action.shift().eq(1)

# replace Percent for not playing row with 1 and then calculate the cumulative product
df['Money'] = '$' + df.Percent.where(df.Playing, 1).cumprod().mul(100).astype(str)

df
#Percent  Action  Playing    Money
#0  1.25       1    False   $100.0
#1  1.20       1     True   $120.0
#2  0.50      -1     True    $60.0
#3  0.75       1    False    $60.0

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 更有效的方法来检查pandas数据框中的相邻行的值以进行股票回测 - More Efficient way to check values of neighboring rows in pandas dataframe for stock backtesting 导入功能并将功能应用于熊猫数据框中的文本数据的更有效方法 - More efficient way to import and apply a function to text data in a Pandas Dataframe 在 Pandas DataFrame 中匹配字符串的更有效方法 - More efficient way to match strings in a Pandas DataFrame 更有效的方法来迭代 groupby Pandas 数据框? - More efficient way to iterate groupby Pandas dataframe? 从字符串(或 DataFrame 对象)中提取时间数据的更有效方法 - More efficient way to pull time data from a string (or DataFrame object) python列表中的pandas数据框以更有效的方式 - pandas data frame from python lists in more efficient way Pandas:在没有for循环的情况下更新pandas数据框中列的更有效方法 - Pandas: More efficient way to update a column in pandas dataframe without a for loop 将熊猫数据帧转换为位图的更有效方法是什么? - What's a more efficient way to convert pandas dataframe to a bit map? 在熊猫数据框列中查找最高值的更有效方法 - More efficient way to find top values in pandas dataframe column 有没有更有效的方法将 XML 文件的目录转换为单个 Pandas Dataframe? - Is there a more efficient way to convert a directory of XML Files to a single Pandas Dataframe?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM