简化了我的问题。我希望创建一个过滤输入价格类型的方法。以下是数据样本:
Open High Low Close
1189.73 1214.01 1188.63 1198.72
1198.67 1201.41 1193.77 1196.35
1195.69 1203.60 1170.78 1176.45
1176.47 1185.01 1172.68 1176.65
adj_price_list = df.values[1:5, [1,2,3,4]].tolist()
['1189.73', '1214.01', '1188.63', '1198.72']
['1198.67', '1201.41', '1193.77', '1196.35']
['1195.69', '1203.6', '1170.78', '1176.45']
['1176.47', '1185.01', '1172.68', '1176.65']
目标:
问题:我无法使用'无'作为参数
adj_price_list = df.values[1:5, [1,4]].tolist()
['1189.73', '1198.72'] #Open and Close prices
adj_price_list = df.values[1:5, [2,3]].tolist()
['1214.01', '1188.63'] #High and Low prices
到rnso:
def GetList(px_open=True, px_high=True, px_low=True, px_close=True):
lst = [px_open, px_high, px_low, px_close]
return [(i+1) if lst[i] else None for i in range(4)]
get_list = GetList(False,True,False,True) #Output: [None, 2, None, 4]
get_price_list = df.values[1:5, get_list].tolist()
IndexError: only integers, slices (`:`), ellipsis (`...`), numpy.newaxis (`None`) and integer or boolean arrays are valid indices
答案 0 :(得分:0)
可以使用以下代替if-else
部分:
def getlist(px_open=True, px_high=True, px_low=True, px_close=True):
lst = [px_open, px_high, px_low, px_close]
return [(i+1) if lst[i] else None for i in range(4)]