希望实现这样一种效果:
从第一行开始,找到满足if条件A的某行(比如index是3),然后从index是3这行向下,找到满足if条件B的某行比如index是25,接着从index是25这行这行向下,找到满足if条件A的某行,如此循环下去。
import pandas as pd
import numpy as np
np.random.seed(20200324)
num = np.random.rand(400).round(2)
data = np.array(num).reshape(100, 4)
df = pd.DataFrame(data, columns=list('ABCD'))
temp_i = 0
for i in range(temp_i, df.shape[0]):
if df.at[i, 'A'] > df.at[i, 'D']:
df.at[i, 'Signal'] = 'num_up'
for i2 in range(i+1, df.shape[0]):
if df.at[i2, 'A'] < df.at[i2, 'D'] * 0.5:
df.at[i2, 'Signal_2'] = 'num_down'
temp_i = i2
break
break
print df
目前只能写出一次的循环,不知道是否有办法可以把现在的代码循环下去,目前我只能用野办法就是把,循环一次的复制了很多很多次,想求问各位高手这种循环怎么去写呢?
import pandas as pd
import numpy as np
np.random.seed(20200324)
num = np.random.rand(400).round(2)
data = np.array(num).reshape(100, 4)
df = pd.DataFrame(data, columns=list('ABCD'))
temp_i = 0
for i in range(temp_i, df.shape[0]):
if df.at[i, 'A'] > df.at[i, 'D']:
df.at[i, 'Signal'] = 'num_up'
for i2 in range(i+1, df.shape[0]):
if df.at[i2, 'A'] < df.at[i2, 'D'] * 0.5:
df.at[i2, 'Signal_2'] = 'num_down'
temp_i = i2
break
break
for i in range(temp_i, df.shape[0]):
if df.at[i, 'A'] > df.at[i, 'D']:
df.at[i, 'Signal'] = 'num_up'
for i2 in range(i+1, df.shape[0]):
if df.at[i2, 'A'] < df.at[i2, 'D'] * 0.5:
df.at[i2, 'Signal_2'] = 'num_down'
temp_i = i2
break
break
for i in range(temp_i, df.shape[0]):
if df.at[i, 'A'] > df.at[i, 'D']:
df.at[i, 'Signal'] = 'num_up'
for i2 in range(i+1, df.shape[0]):
if df.at[i2, 'A'] < df.at[i2, 'D'] * 0.5:
df.at[i2, 'Signal_2'] = 'num_down'
temp_i = i2
break
break
print df