pandas.cut:
pandas.cut(x, bins, right=True, labels=None, retbins=False, precision=3, include_lowest=False)
參數:
- x,類array對象,且必須爲一維,待切割的原形式
- bins, 整數、序列尺度、或間隔索引。如果bins是一個整數,它定義了x寬度範圍內的等寬面元數量,但是在這種情況下,x的範圍在每個邊上被延長1%,以保證包括x的最小值或最大值。如果bin是序列,它定義了允許非均勻bin寬度的bin邊緣。在這種情況下沒有x的範圍的擴展。
- right,布爾值。是否是左開右閉區間
- labels,用作結果箱的標籤。必須與結果箱相同長度。如果FALSE,只返回整數指標面元。
- retbins,布爾值。是否返回面元
- precision,整數。返回面元的小數點幾位
- include_lowest,布爾值。第一個區間的左端點是否包含
返回值:
若labels爲False則返回整數填充的Categorical或數組或Series
若retbins爲True還返回用浮點數填充的N維數組
demo:
>>> pd.cut(np.array([.2, 1.4, 2.5, 6.2, 9.7, 2.1]), 3, retbins=True)
...
([(0.19, 3.367], (0.19, 3.367], (0.19, 3.367], (3.367, 6.533], ...
Categories (3, interval[float64]): [(0.19, 3.367] < (3.367, 6.533] ...
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
>>> 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])
pandas.qcut
pandas.qcut(x, q, labels=None, retbins=False, precision=3, duplicates=’raise’)
參數:
1.x
2.q,整數或分位數組成的數組。
3.labels,
4.retbins
5.precisoon
6.duplicates
結果中超過邊界的值將會變成NA
demo:
>>> pd.qcut(range(5), 4)
...
[(-0.001, 1.0], (-0.001, 1.0], (1.0, 2.0], (2.0, 3.0], (3.0, 4.0]]
Categories (4, interval[float64]): [(-0.001, 1.0] < (1.0, 2.0] ...
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
>>> pd.qcut(range(5), 3, labels=["good", "medium", "bad"])
...
[good, good, medium, bad, bad]
Categories (3, object): [good < medium < bad]
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
pd.qcut(range(5), 4, labels=False)
array([0, 0, 1, 2, 3])