0
我正在处理大型数据集,我需要查看同一列中的下一行值是否大于当前值。然后保存1或-1。因此,如果col d中的当前行为1,并且同一col中的下一个值为2,则它会在同一行和同一数据帧中的新列('e)上保存1。问题是它总是保存一个单一的值。DataFrame不保存正确的值
import numpy as np
import pandas as pd
df1 = pd.DataFrame(np.random.randn(10, 4), columns=['a', 'b', 'c', 'd'])
mask = df1.applymap(lambda x: x <-0.7)
df1 = df1[-mask.any(axis=1)]
sLength = len(df1['a'])
rwno=0
PrevClose=[]
for index,row in df1.iterrows():
Close=row.iloc[3]
PrevClose.append(Close)
rwno+=1
print df1
rwno=1
for index,row in df1.iterrows():
NxtDaySpy=0
if rwno < len(df1.index) :
NextClose=PrevClose[rwno]
Close=row.iloc[3]
df1['e']=pd.Series((NextClose-Close)/abs(NextClose-Close), index=df1.index)
rwno+=1
print df1.head
我以一种不同的方式使用了这个逻辑,但是这个工作很完美。 – newtooca