如何在pandas .value()函数中输入空参数

问题描述 投票:0回答:1

简化了我的问题。我希望创建一个过滤输入价格类型的方法。以下是数据样本:

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']

目的:

  • 一种更智能的方法来过滤O / H / I / C输入选择。目前使用多个if-else语句。
  • 问题:我不能使用'None'作为参数 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
python pandas numpy trading algorithmic-trading
1个回答
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)]
© www.soinside.com 2019 - 2024. All rights reserved.