How to implement Percentile groups with python
Editor to share with you how python to achieve Percentile groups, I believe that most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to know it!
Percentile groups
You have a number column, and you want to classify the values in this column into groups, for example, the first 5% of the column is divided into group 1, the first 5-20% is divided into group 2, the first 20% of 50% is divided into group 3, and the last 50% is divided into group 4. Of course, you can do it with pandas.cut, but here's another option:
Import numpy as np cut_points = [np.percentile (df ['c'], I) for i in [50,80,95]] df ['group'] = 1 for i in range (3): df [' group'] = df ['group'] + (df [' c'] < cut_ points [I]) # or