DECROMAX

Assign string values based on multiple ranges

Sep 11th, 2021
338
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.52 KB | None | 0 0
  1. df['new_col'] = pd.cut(df['existing_col'],
  2.                              bins=[1, 5, 12, 18, 24, 30, np.inf],  # bins = ranges ie 1, 5 = 1 to 5
  3.                              labels=['1 to 5', '5 to 12', '12 to 18', # labels map to the bin ranges ie: 1, 5 = '1 to 5'
  4.                                      '18 to 24', '24 to 30', '>30'],
  5.                              right=False)
  6. # If you're going to use cut use np.inf not an arbitrary upper bound value of 999999. OP's function is also lowerbound  inclusive so need right=False
Advertisement
Add Comment
Please, Sign In to add comment