2

I want to make a calculation for each item in a list. Then, I want to add these new values to a column in a dataframe. The name of the column should be: name_item This is what I have so far:

list_thr = [0.1, 0.5]


for n in list_thr:
    y_scores = (best_classifier.predict_proba(X_test)[:,1]>=n).astype(int) 
    probs_score = best_classifier.predict_proba(X_test)

    df_final['preds'] = y_scores #df_final is the dataframe I want to put the columns

However, if I keep it, the column 'preds' will be replaced in each iteraction. What I want is 1 column for each iteraction and with "n" in the name, such as:

df_final:

preds_0.1   preds_0.5
1           1
0           0
0           1
0           0
1           1

How can I change my loop to get that?

1 Answer 1

3

You can add the n when iterating as part of the column name when you define it. This will create a new column with the name preds and the suffix n for each loop:

for n in list_thr:
    y_scores = (best_classifier.predict_proba(X_test)[:,1]>=n).astype(int) 
    probs_score = best_classifier.predict_proba(X_test)

    df_final['preds'+str(n)] = y_scores
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.