使用裝箱(bin)欄位數值分組問題
題目說分成4小組,0, 1~2 , 3~5 , 5以上 ,但我嘗試過發現他切割分組的情況會變這樣~
如果我寫cut_rule =[1,2,3,5]
他就會被切割成:(1,2] , (2,3] ,(3,5] ,其他等於0或是大於5的都會變成NaN,而且(1,2] 並不包含2,也就是2會被分至(2,3] 的組別。
所以我就以下方圖的方式分割,不知是否正確?
由請各位大大指教!
回答列表
-
2019/05/13 下午 10:35張維元 (WeiYuan)贊同數:1不贊同數:0留言數:0
通常這種分組的做法我們稱為裝箱(bin),pd.cut(data, bins=bin_cut) 中 bin_cut 代表是怎麼裝,實際用法我們可以參考文件:https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.cut.html
bins 可以接受 int、 sequence 或 IntervalIndex,使用差異如下:
* int : Defines the number of equal-width bins in the range of x. The range of x is extended by .1% on each side to include the minimum and maximum values of x.
* sequence of scalars : Defines the bin edges allowing for non-uniform width. No extension of the range of x is done.
除了給一個規則之外,也可以直接給 bin = 4 ,就會幫你分成四組