简体   繁体   中英

Python pandas extract variables from dataframe

What is the best way to convert DataFrame columns into variables. I have a condition for bet placement and I use 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)

I would like to convert columns into variables and pass them to API for bet placement. So I convert back_bf_lay_bq to dictionary and extract values

    #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)

I do not think that it is the most efficient way and it brings a bug from time to time
bq_selection_id = dd['bq_selection_id'][0] IndexError: list index out of range

So can you suggest a better way to get values from DataFrame and pass them to API?

IIUC you could use iloc to get your first row and then slice your dataframe with your columns subset and pass that to your variables. Something like that:

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]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM