首页 > 解决方案 > 如何获取 Python 数据框中的下一行值?

问题描述

我是一个新的 Python 用户,我正在努力学习这一点,以便我可以完成一个关于加密货币的研究项目。我想要做的是在找到条件后立即检索值,然后在另一个变量中检索 7 行的值。

我正在使用具有 2250 行和 25 列的 Excel 电子表格。通过添加下面详述的 4 列,我得到 29 列。它有很多 0(没有发现模式)和几个 100(发现模式)。我希望我的程序在出现 100 的那一行之后立即获取该行,并返回它的收盘价。这样,我可以看到模式当天和模式后一天之间的差异。我也想在接下来的 7 天里这样做,以找到一周的模式表现。

这是电子表格的屏幕截图来说明这一点

您也可以看到 -100 个单元格,这些是看跌模式识别。现在我只想使用“100”个单元格,这样我至少可以完成这项工作。

我希望发生这种情况:

import pandas as pd
import talib
import csv
import numpy as np

my_data = pd.read_excel('candlesticks-patterns-excel.xlsx')
df = pd.DataFrame(my_data)

df['Next Close'] = np.nan_to_num(0) #adding these next four columns to my dataframe so I can fill them up with the later variables#
df['Variation2'] = np.nan_to_num(0)
df['Next Week Close'] = np.nan_to_num(0)
df['Next Week Variation'] = np.nan_to_num(0)
df['Close'].astype(float)

for row in df.itertuples(index=True):
    str(row[7:23])
    if ((row[7:23]) == 100):
        nextclose = np.where(row[7:23] == row[7:23]+1)[0] #(I Want this to be the next row after having found the condition)#
    if (row.Index + 7 < len(df)):
        nextweekclose = np.where(row[7:23] == row[7:23]+7)[0] #(I want this to be the 7th row after having found the condition)#
    else:
        nextweekclose = 0

我想要这些值的原因是稍后将它们与这些变量进行比较:

variation2 = (nextclose - row.Close) / row.Close * 100
    nextweekvariation = (nextweekclose - row.Close) / row.Close * 100
    df.append({'Next Close': nextclose, 'Variation2': variation2, 'Next Week Close': nextweekclose, 'Next Week Variation': nextweekvariation}, ignore_index = true)

我的错误来自于我不知道如何检索 row+1 值和 row+7 值。我整天在网上搜索高低,并没有找到具体的方法来做到这一点。无论我尝试想出的哪个想法都会给我一个“只能将元组(而不是“int”)连接到元组”错误,或者一个“AttributeError:'Series' object has no attribute 'close'”。我尝试时得到的第二个:

for row in df.itertuples(index=True):
    str(row[7:23])
    if ((row[7:23]) == 100):
        nextclose = df.iloc[row.Index + 1,:].close
    if (row.Index + 7 < len(df)):
        nextweekclose = df.iloc[row.Index + 7,:].close
    else:
        nextweekclose = 0

我真的很想在这方面提供一些帮助。使用 Jupyter 笔记本。

编辑:固定

我终于成功了!编程似乎经常出现这种情况(是的,我是新来的......),错误是因为我无法跳出框框思考。当问题比这更深时,我被说服了我的代码的某个部分是问题所在。

感谢 BenB 和 Michael Gardner,我已经修复了我的代码,现在它正在返回我想要的。这里是。

import pandas as pd
import talib
import csv
import numpy as np
        
my_data = pd.read_excel('candlesticks-patterns-excel.xlsx')
df = pd.DataFrame(my_data)
        
        
#Creating my four new columns. In my first message I thought I needed to fill them up
#with 0s (or NaNs) and then fill them up with their respective content later. 
#It is actually much simpler to make the operations right now, keeping in mind 
#that I need to reference df['Column Of Interest'] every time.
    
df['Next Close'] = df['Close'].shift(-1)
df['Variation2'] = (((df['Next Close'] - df['Close']) / df['Close']) * 100)
df['Next Week Close'] = df['Close'].shift(-7)
df['Next Week Variation'] = (((df['Next Week Close'] - df['Close']) / df['Close']) * 100)
    
#The only use of this is for me to have a visual representation of my newly created columns#
print(df)
        
for row in df.itertuples(index=True):
    if 100 or -100 in row[7:23]:
        nextclose = df['Next Close']
            
    if (row.Index + 7 < len(df)) and 100 or -100 in row[7:23]:
            nextweekclose = df['Next Week Close']
        else:
            nextweekclose = 0
                
        variation2 = (nextclose - row.Close) / row.Close * 100
        nextweekvariation = (nextweekclose - row.Close) / row.Close * 100
        df.append({'Next Close': nextclose, 'Variation2': variation2, 'Next Week Close': nextweekclose, 'Next Week Variation': nextweekvariation}, ignore_index = True)
        
df.to_csv('gatherinmahdata3.csv')

标签: pythonpandasdataframerowsta-lib

解决方案


如果我理解正确,您应该能够使用shift您想要的数量移动行,然后进行条件计算。

import pandas as pd
import numpy as np

df = pd.DataFrame({'Close': np.arange(8)})

df['Next Close'] = df['Close'].shift(-1)
df['Next Week Close'] = df['Close'].shift(-7)

df.head(10)

   Close  Next Close  Next Week Close
0      0         1.0              7.0
1      1         2.0              NaN
2      2         3.0              NaN
3      3         4.0              NaN
4      4         5.0              NaN
5      5         6.0              NaN
6      6         7.0              NaN
7      7         NaN              NaN

df['Conditional Calculation'] = np.where(df['Close'].mod(2).eq(0), df['Close'] * df['Next Close'], df['Close'])

df.head(10)

   Close  Next Close  Next Week Close  Conditional Calculation
0      0         1.0              7.0                      0.0
1      1         2.0              NaN                      1.0
2      2         3.0              NaN                      6.0
3      3         4.0              NaN                      3.0
4      4         5.0              NaN                     20.0
5      5         6.0              NaN                      5.0
6      6         7.0              NaN                     42.0
7      7         NaN              NaN                      7.0

推荐阅读