仅使用一个功能遍历列

时间:2018-07-10 13:34:15

标签: python python-3.x python-2.7 function loops

我有一个Trend_1,Trend_2,Trend_3 .... to到Trend_2_3。因此,我要运行一个返回信号的函数。该函数运行良好,只是我必须像重复下面的代码一样多次,只是更改名称。代码是相同的,只是我必须从df ['Signal_1']重复到df ['Signal_2_3']手动。只是想知道是否有其他方法可以返回可以立即读取趋势列的函数 My dataframe

这是我的功能代码,它向我返回signal_1到signal_2_3

def Signal_processing(df):

signal_1= [None]
for i in range (1,len(df)):
    if df['Trend_1'][i]== 'Up' and df['Trend_1'][i] != df['Trend_1'][i-1]:
        signal_1 += ['Buy']
    elif df['Trend_1'][i] =='Down' and df['Trend_1'][i]!= df['Trend_1'][i-1]:
        signal_1 += ['Sell']
    else :
        signal_1 += [None]

df['Signal_1'] = signal_1 

index=[]
bs=[]
for i in range (len(df)):
    if df['Signal_1'][i] == 'Buy' or df['Signal_1'][i] == 'Sell':
        bs.append(df['Signal_1'][i])
        index.append(i)

for i in range (1,len(bs)):
    if bs[i] == bs[i-1]:
        df['Signal_1'][index[i]]='None'

这样的代码将重复7次,但名称不同。

signal_7=[None]
for i in range (1,len(df)):
    if df['Trend_2_3'][i]=='Up' and df['Trend_2_3'][i]!= df['Trend_2_3'][i-1] :
        signal_7 += ['Buy']
    elif df['Trend_2_3'][i] == 'Down' and df['Trend_2_3'][i]!= df['Trend_2_3'] [i-1]:
        signal_7 += ['Sell']
    else:
        signal_7 += [None]
df['Signal_2_3'] = signal_7   

    index=[]
bs=[]
for i in range (len(df)):
    if df['Signal_2_3'][i] == 'Buy' or df['Signal_2_3'][i] == 'Sell':
        bs.append(df['Signal_2_3'][i])
        index.append(i)

for i in range (1,len(bs)):
    if bs[i] == bs[i-1]:
        df['Signal_2_3'][index[i]]='None'              

return df 

1 个答案:

答案 0 :(得分:1)

您可以仅从以下位置更改函数调用

def Signal_processing(df):
    # do some stuff for all trends

def Signal_processing(df):
    sub_signal_processing(df,"1")
    sub_signal_processing(df,"2")
    sub_signal_processing(df,"2_3")
    #etc

def sub_signal_processing(df, no):
    # do some stuff for a specific trend specified by the string 'no'
    # If no = "7", then Trend_7 will be processed to Signal_7,
    # If no = "2_3", then Trend_2_3 will be processed to Signal_2_3 etc.

,然后将每个实例更改为例如趋势_2_3和信号_2_3通过以下方式:

# Former specific call
df['Trend_2_3']
df['Signal_2_3']
# The same call if no = "2_3"
df['Trend_%s' % no]
df['Signal_%s' % no]

其中no是一个字符串。然后,您需要将Signal_processing中的所有内容移至处理过程,例如将Trend_1细分为sub_signal_processing,并用上面指定的通用版本替换_1。下面的代码应执行您想要的操作:

def Signal_processing(df):
    # Could add more here
    for no in ("1", "2", "3", "1_2_3", "1_2", "1_3", "2_3"):
        sub_signal_processing(df, no)

def sub_signal_processing(df, no):
    signal= [None]
    for i in range (1,len(df)):
        if df['Trend_%s' % no][i]== 'Up' and df['Trend_%s' % no][i] != df['Trend_%s' % no][i-1]:
            signal += ['Buy']
        elif df['Trend_%s' % no][i] =='Down' and df['Trend_%s' % no][i]!= df['Trend_%s' % no][i-1]:
            signal += ['Sell']
        else :
            signal += [None]

    df['Signal_%s' % no] = signal

    index=[]
    bs=[]
    for i in range (len(df)):
        if df['Signal_%s' % no][i] == 'Buy' or df['Signal_%s' % no][i] == 'Sell':
            bs.append(df['Signal_%s' % no][i])
            index.append(i)

    for i in range (1,len(bs)):
        if bs[i] == bs[i-1]:
            df['Signal_%s' % no][index[i]]='None'