Pandas中的cut函數用于將連續(xù)的數據劃分為離散的間隔。其語法如下:
pandas.cut(x, bins, right=True, labels=None, retbins=False, precision=3, include_lowest=False, duplicates='raise')
參數說明:
示例:
import pandas as pd
data = [1, 5, 10, 15, 20, 25]
bins = [0, 10, 20, 30]
result = pd.cut(data, bins)
print(result)
輸出:
[(0, 10], (0, 10], (0, 10], (10, 20], (10, 20], (20, 30]]
Categories (3, interval[int64]): [(0, 10] < (10, 20] < (20, 30]]
在上面的示例中,我們將數據data劃分為3個間隔,分別為(0, 10],(10, 20],(20, 30]。cut函數返回一個Categorical對象,顯示了每個值所屬的間隔。