Slice Age in Python
>>> pd.cut(np.array([.2, 1.4, 2.5, 6.2, 9.7, 2.1]), 3, retbins=True) ([(0.191, 3.367], (0.191, 3.367], (0.191, 3.367], (3.367, 6.533], (6.533, 9.7], (0.191, 3.367]] Categories (3, object): [(0.191, 3.367] < (3.367, 6.533] < (6.533, 9.7]], array([ 0.1905 , 3.36666667, 6.53333333, 9.7 ])) >>> pd.cut(np.array([.2, 1.4, 2.5, 6.2, 9.7, 2.1]), 3, labels=["good","medium","bad"]) [good, good, good, medium, bad, good] Categories (3, object): [good < medium < bad] >>> pd.cut(np.ones(5), 4, labels=False) array([1, 1, 1, 1, 1], dtype=int64)
Here is what the above code is Doing:
1. pd.cut(np.array([.2, 1.4, 2.5, 6.2, 9.7, 2.1]), 3, retbins=True)
– This is creating an array of numbers and then cutting them into 3 bins.
– The retbins=True is returning the bins that were created.
2. pd.cut(np.array([.2, 1.4, 2.5, 6.2, 9.7, 2.1]), 3, labels=[“good”,”medium”,”bad”])
– This is creating an array of numbers and then cutting them into 3 bins.
– The labels=[“good”,”medium”,”bad”] is labeling the bins.
3. pd.cut(np.ones(5), 4, labels=False)
– This is creating an array of ones and then cutting them into 4 bins.
– The labels=False is not labeling the bins.