Pandas中的cut函数用于将连续的数据划分为离散的间隔。其语法如下:
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对象,显示了每个值所属的间隔。