循环通过pandas数据帧

时间:2016-01-23 01:19:07

标签: python pandas dataframe

我试图重新测试投资策略。我无法循环使用DataFrame来“重新创建”战略从15年前开始实施的方式。当我尝试循环遍历df ['Average_Diff']时,我不断收到错误“列表索引必须是整数或切片,而不是numpy.float64”。由于['Average_Diff']的值是如何计算的,我一直在努力处理在列开头会发生的nan,但是一旦我修复了我遇到了另一个问题。那么我如何循环通过df ['Average_Diff']创建“买入或卖出”信号并循环显示我是基于“信号”进入市场还是退出市场?

import pandas as pd 
import pandas.io.data 
from pandas import Series, DataFrame
import datetime
from pandas import ExcelWriter 
import os 
import matplotlib.pyplot as plt 
import math
import numpy as np 
from numpy import *
now = datetime.datetime.now()


start_of_interval = datetime.datetime(now.year - 15, now.month, now.day)
end_of_interval = datetime.datetime(now.year, now.month, now.day)       
df = pd.io.data.get_data_yahoo("Spy", start = start_of_interval, end = end_of_interval, interval = "d")['Adj Close']

df = DataFrame(df) 
df['Returns'] = df.pct_change()
df['Average_200'] = pd.rolling_mean(df['Adj Close'],200)
df['Average_50'] = pd.rolling_mean(df['Adj Close'],50) 
df['Date'] = df.index 

df['Average_Diff'] = df['Average_50'] - df['Average_200']  
df['Average_Diff'] = df['Average_Diff'].fillna(int(2)) 
print(df) 
for i in df['Average_Diff']:
    if df['Average_Diff'][i] == int(2):
    df["Signal"] = "Hold"
    df["Market"] = 1
if df['Average_Diff'][i-1] > 0 and ['Average_Diff'][i] < 0:
    df["Signal"] = "Buy"
    df['Market'] = 1
elif df['Average_Diff'][i-1] < 0 and ['Average_Diff'][i] > 0:
    df["Signal"] = "Sell"
    df["Signal"] = 0 
else:
    df["Signal"] = "Hold" 

for i in df["Market"]:
    if df["Signal"][i] == "Sell":
        df["Market2"] = 0
    elif df['Signal'][i] == "Hold" and df['Market'][i-1] == 0:
        df['Market2'] = 0
    elif df['Signal'][i] == "Hold" and df['Market'][i-1] == 1:  
        df['Market2'] = 1 
    elif df['Signal'][i] == "Buy":
        df['Market2'] = 1 
    else:
        df["Market2"] = 1

1 个答案:

答案 0 :(得分:1)

您可以尝试以下几种方法:

l = len(df)
for i in range(len):
    if df.loc[i, 'Average_Diff'] == int(2):
        df.loc[i, 'Signal'] = 'Hold'
        df.loc[i, 'Market'] = 1

或者(更喜欢这个,超过上面的那个)

for i in df.index.values:
    if df.loc[i, 'Average_Diff'] == int(2):
        df.loc[i, 'Signal'] = 'Hold'
        df.loc[i, 'Market'] = 1

修改

l = df.index.values
for i in range(1, len(l)):
    if df.loc[l[i], 'Average_Diff'] == int(2):
        df.loc[l[i], 'Signal'] = 'Hold'
        df.loc[l[i], 'Market'] = 1
    # Even i-1 will work in the same way: l[i-1] 

与评论相反:

  

您应该永远不会修改您正在迭代的内容。这不是   保证在所有情况下都有效。根据数据类型,   迭代器返回一个副本而不是一个视图,写入它将没有   影响。 1