简体   繁体   English

从元组列表中,将元组 Index[0] 和 Index[1] 插入到函数中

[英]From a tuple list, Insert tuples Index[0] and Index[1] into a function

Good mooring to all,祝大家停泊好,

The objective is to be able to create a series of new columns by inserting x and y into the df[f'sma_{x} Vs_sma {y}'] function.目标是能够通过将 x 和 y 插入 df[f'sma_{x} Vs_sma {y}'] 函数来创建一系列新列。

The problem that I'm having is that I'm only getting the last tuple value into the function and therefore into the data frame as you can see on the last image.我遇到的问题是我只将最后一个元组值放入函数中,因此放入数据框中,如您在最后一张图像上看到的那样。

On the second part of the code, 3 examples on how the tuples values must be plug into the function.在代码的第二部分,3 个关于如何将元组值插入函数的示例。 IN the examples I will be using the first 2 tuples (10,11), (10,12) and the last tuple (48,49)在示例中,我将使用前 2 个元组 (10,11)、(10,12) 和最后一个元组 (48,49)

Code:代码:

a = list(combinations(range(10, 15),2))
print(a)

for index, tuple in enumerate(a):
    x = tuple[0]
    y = tuple[1]
    print(x, y)

df[f'sma_{x}_Vs_sma_{y}'] = np.where(ta.sma(df['close'], lenght = x)  > ta.sma(df['close'], lenght = y),1,-1)

Code Examples:代码示例:

Tuple (10,11)元组 (10,11)

df[f'sma_{10}_Vs_sma_{11}'] = np.where(ta.sma(df['close'], lenght = 10)  > ta.sma(df['close'], lenght = 11),1,-1)

Tuple (10,12)元组 (10,12)

df[f'sma_{10}_Vs_sma_{12}'] = np.where(ta.sma(df['close'], lenght = 10)  > ta.sma(df['close'], lenght = 12),1,-1)

Tuple (13,14)元组 (13,14)

df[f'sma_{13}_Vs_sma_{14}'] = np.where(ta.sma(df['close'], lenght = 13)  > ta.sma(df['close'], lenght = 14),1,-1)

Error code错误代码

On the next lines the code that solve the issue.在下一行是解决问题的代码。 Although looking backwards seams very easy, it took me some time to get to the answer.虽然向后看接缝很容易,但我花了一些时间才找到答案。

Thanks to the people that comment on the issue感谢对这个问题发表评论的人

a = list(combinations(range(5, 51),2))
print(a)

for x, y in a :
  df[f'hma_{x}_Vs_hma_{y}'] = np.where(ta.hma(df['close'], lenght = x)  > ta.hma(df['close'], lenght = y),1,-1)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM