繁体   English   中英

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

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

祝大家停泊好,

目标是能够通过将 x 和 y 插入 df[f'sma_{x} Vs_sma {y}'] 函数来创建一系列新列。

我遇到的问题是我只将最后一个元组值放入函数中,因此放入数据框中,如您在最后一张图像上看到的那样。

在代码的第二部分,3 个关于如何将元组值插入函数的示例。 在示例中,我将使用前 2 个元组 (10,11)、(10,12) 和最后一个元组 (48,49)

代码:

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)

代码示例:

元组 (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)

元组 (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)

元组 (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)

错误代码

在下一行是解决问题的代码。 虽然向后看接缝很容易,但我花了一些时间才找到答案。

感谢对这个问题发表评论的人

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