Home > Software engineering >  binning with multiple conditions in pandas dataframe
binning with multiple conditions in pandas dataframe

Time:10-22

Existing Dataframe :

Id       Total_amt      amt_paid    pending_amt        

A          100             0           100
B          200            20           180
C          500            500           0
D          1000           400          600
E          2000           100          1900

Expected Dataframe :

Id       Total_amt      amt_paid    pending_amt     status    

A          100             0           100        No_Payment_Made
B          200            20           180            < 250
C          500            500           0         Payment_Completed
D          1000           400          600           250-750
E          2000           100          1900         > 750

I am trying to Categorize status into five categories i.e. No_Payment_Made , Payment_Completed , < 250 , 250-750 and > 750

tried binning the pending_amt with pd.cut but stuck with how to form bins and labels to get desired output

CodePudding user response:

Try use np.select

col = 'pending_amt'
col1 = 'Total_amt'

conditions = [df[col] == df[col1], (df[col] < 250) & (df[col] != 0), (df[col] >= 250) & (df[col] <= 750), df[col] > 750, df[col] == 0]

result = ['No_Payment_Made','<250','250-750','>750','Payment_Completed']

df['status'] = np.select(conditions, result, default='REST')
  • Related