将DataFrame列转换为变量的最佳方法是什么。我有下注的条件,我使用head(n = 1)
back_bf_lay_bq = bb[(bb['bf_back_bq_lay_lose_net'] > 0) & (bb['bq_lay_price'] < 5) & (bb['bq_lay_price'] != 0) & (bb['bf_back_liquid'] > bb['bf_back_stake']) & (bb['bq_lay_liquid'] > bb['bq_lay_horse_win'])].head(n=1)
我想将列转换为变量并将其传递给API以进行下注。所以我将back_bf_lay_bq转换为字典并提取值
#Bets placements
dd = pd.DataFrame.to_dict(back_bf_lay_bq, orient='list')
#Betdaq bet placement
bq_selection_id = dd['bq_selection_id'][0]
bq_lay_stake = dd['bq_lay_stake'][0]
bq_lay_price = dd['bq_lay_price'][0]
bet_type = 2
reset_count = dd['bq_count_reset'][0]
withdrawal_sequence = dd['bq_withdrawal_sequence'][0]
kill_type = 2
betdaq_request = betdaq_api.PlaceOrdersNoReceipt(bq_selection_id,bq_lay_stake,bq_lay_price,bet_type,reset_count,withdrawal_sequence,kill_type)
我不认为这是最有效的方式,它不时会带来一个错误 bq_selection_id = dd ['bq_selection_id'] [0] IndexError:列表索引超出范围
那么你能建议一种更好的方法从DataFrame获取值并将它们传递给API吗?
答案 0 :(得分:0)
IIUC您可以使用iloc
获取第一行,然后使用您的列子集对数据帧进行切片并将其传递给您的变量。这样的事情:
bq_selection_id, bq_lay_stake, bq_lay_price, withdrawal_sequence = back_bf_lay_bq[['bq_selection_id', 'bq_lay_stake', 'bq_lay_price', 'withdrawal_sequence']].iloc[0]