首页 > 解决方案 > 从组合列表中,将元组 Index[0] 和 Index[1] 插入到函数中

问题描述

目标是通过将不同元组中的 index[0] 和 index[1] 插入到代码末尾的 CHOICES 函数和 DF 函数中来创建一系列新列。

问题是我只在函数内部获取第一个元组(10 和 11),因此我只在数据框(sma_10 和 sma_11)中获取 1 列,而不是 30 或更多新列

在代码的第二部分,3 个示例说明了如何使用前 2 个元组 (10,11)、(10,12) 和最后一个元组 (48,49) 将索引插入到函数中

我做错了什么?任何想法,线索或评论都超过赞赏

原始代码:

import pandas as pd
import pandas_ta as ta
import numpy as np
from itertools import product, combinations

b = list(combinations(range(10, 50),2))

x = b[0]
y = b[1]

print((b))
print(list(x))
print((y[1]))

conditions = [df[f'sma_{x[0]}'] > df[f'sma_{x[1]}'],
             df[f'sma_{x[0]}'] < df[f'sma_{x[1]}']]

choices = [1, -1]

df[f'sma_{x[0]} & sma_{x[1]}'] = np.select(conditions, choices, default=0)

第二部分

元组 (10,11)

conditions = [df[f'sma_{10}’] > df[f'sma_{11}'],
                 df[f'sma_{10}’] < df[f'sma_{11}']]
    
    choices = [1, -1]
    
    df[f'sma_{10} & sma_{11}’] = np.select(conditions, choices, default=0)

元组 (10,12)

conditions = [df[f'sma_{10}’] > df[f'sma_{12}’],
                 df[f'sma_{10}’] < df[f'sma_{12}’]]


choices = [1, -1]

df[f'sma_{10} & sma_{12}’] = np.select(conditions, choices, default=0)

元组 (48,49)

conditions = [df[f'sma_{48}’] > df[f'sma_{49}’],
                 df[f'sma_{48}’] < df[f'sma_{49}']]
    
    choices = [1, -1]
    
    df[f'sma_{48} & sma_{49}’] = np.select(conditions, choices, default=0)

标签: pythontuplesrangecombinationsitertools

解决方案


推荐阅读