'changing value in a column if value is greater than 0.6 in another column pandas [duplicate]

I have two columns in dataframe one is prediction_class which is having 2 values 0 and 1 another column is predict_probabilty which is having values between 0 and 1. I want to change value in prediction_class to 0 if value in predict_probabilty is lesser than 0.60 and value in prediction_class to 1 if value in predict_probabilty is greater than 0.60.

Dataframe looks like this:

prediction_class   predict_probabilty
      1                  0.90
      1                  0.85
      0                  0.75
      0                  0.89
  


Solution 1:[1]

Use numpy.where:

import numpy as np

df['prediction_class'] = np.where(df['predict_probabilty'].lt(0.6), 0, 1)

Solution 2:[2]

Compre for greater like 0.6 and cast mask to integers:

df['prediction_class'] = df['predict_probabilty'].gt(0.6).astype(int)

Or:

df['prediction_class'] = df['predict_probabilty'].gt(0.6).view('i1')

Or:

df['prediction_class'] = np.where(df['predict_probabilty'].gt(0.6), 1, 0)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Mayank Porwal
Solution 2