简体   繁体   中英

Pandas.cut specify custom range

Is it possible to specify a custom range in pandas.cut ?

I have a dataset where I need to bin the age column (and several other columns). The min and the max value of age in the dataset is 18 and 55 , respectively. However, in the documentation of the dataset, it is written that the range of the attribute age is 18-58 .

pandas.cut will automatically bin according to the range of the dataset (which will be 18-55 ), which is wrong.

Is there any way I can specify the range to bin on in the pandas.cut method? I looked into IntervalIndex tuples as bins, but that would mean that I generate the bins manually myself. I am looking for if pandas.cut has this functionality built-in.

No, not directly.

However, you could pass np.linspace(18, 58, n_bins) to pd.cut . I'm not sure if you consider that "generating manually".

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM