簡體   English   中英

帶有其他參數的Pandas df.apply

[英]Pandas df.apply with additional arguments

我正在嘗試在熊貓中應用一個函數,該函數要求我使用其他變量進行評分。 例:

def get_score_threshold(pred_df):
    score_thresholds = {}
    score_thresholds['a_thresh'] = np.percentile(pred_df['pred'],75)
    score_thresholds['b_thresh'] = np.percentile(pred_df['pred'],50)
    score_thresholds['c_thresh'] = np.percentile(pred_df['pred'],25)
    return score_thresholds

def score_pred(pred,scores):
    if pred >= scores['a_thresh']:
        return 'A'
    elif pred >= scores['b_thresh']:
        return 'B'
    elif pred >= scores['c_thresh']:
        return 'C'
    else: return 'D'

close_preds['score'] = close_preds['pred'].apply(score_pred(scores=close_pred_thresh))

如何指定第一個變量是要傳遞的行,第二個變量是我提供的附加參數?

文檔中看來

close_preds['score'] = close_preds['pred'].apply(score_pred, args=(pred, scores))

我看不到你的close_pred_thresh是什么。 score_pred需要兩個參數。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM