Python implementation of discrete processing of continuous data is mainly based on two functions, and, the former according to the specified cut-off point for continuous data box, the latter can be based on the number of specified boxes of continuous data for equal width box processing, the so-called equal width refers to the amount of data in each box is the same.
The following is a brief description of the usage of these two functions:
# Import pandas package import pandas as pd ages = [20, 22, 25, 27, 21, 23, 37, 31, 61, 45, 41, 32] # of cartons to be distributed bins = [18, 25, 35, 60, 100] # Specify the cut-off point of the box
Functions :
cats1 = (ages, bins) cats1
cats1 Results:
[(18, 25], (18, 25], (18, 25], (25, 35], (18, 25], ..., (25, 35], (60, 100], (35, 60], (35, 60], (25, 35]] Length: 12 Categories (4, interval[int64]): [(18, 25] < (25, 35] < (35, 60] < (60, 100]] # labels parameter is False, different integers are used as box indicators in the returned result cats2 = (ages, bins,labels=False) cats2 # The numbers in the output correspond to different boxes
cats2 results:
array([0, 0, 0, 1, 0, 0, 2, 1, 3, 2, 2, 1], dtype=int64) pd.value_counts(cats1) # Counting numbers in different boxes
Counting results:
(18, 25] 5 (35, 60] 3 (25, 35] 3 (60, 100] 1 dtype: int64 (ages, [18, 26, 36, 61, 100], right=False) # Specify that the splitter box interval is left-closed and right-open
Change the zone opening and closing results:
[[18, 26), [18, 26), [18, 26), [26, 36), [18, 26), ..., [26, 36), [61, 100), [36, 61), [36, 61), [26, 36)] Length: 12 Categories (4, interval[int64]): [[18, 26) < [26, 36) < [36, 61) < [61, 100)] # The labels you want to assign to different boxes can be passed to the labels parameter group_names = ['Youth', 'YoungAdult', 'MiddleAged', 'Senior'] cuts3 = (ages, bins, labels=group_names) cuts3
cats3 results:
[Youth, Youth, Youth, YoungAdult, Youth, ..., YoungAdult, Senior, MiddleAged, MiddleAged, YoungAdult] Length: 12 Categories (4, object): [Youth < YoungAdult < MiddleAged < Senior]
function:
qcats1 = (ages,q=4) # Parameter q specifies the number of boxes to be divided qcats1
qcats1 results:
[(19.999, 22.75], (19.999, 22.75], (22.75, 29.0], (22.75, 29.0], (19.999, 22.75], ..., (29.0, 38.0], (38.0, 61.0], (38.0, 61.0], (38.0, 61.0], (29.0, 38.0]] Length: 12 Categories (4, interval[float64]): [(19.999, 22.75] < (22.75, 29.0] < (29.0, 38.0] < (38.0, 61.0]] qcats1.value_counts() # As you can see from the output, the amount of data in each box is the same.
Counting results:
(19.999, 22.75] 3 (22.75, 29.0] 3 (29.0, 38.0] 3 (38.0, 61.0] 3 dtype: int64
Ref: Data Analysis with Python - Wes McKinney 2nd Edition
Above this use of pandas to achieve continuous data discrete processing mode (split-box operation) is all that I have shared with you, I hope to be able to give you a reference, and I hope you will support me more.