>>> pd.qcut(range(5), 4, labels=False)
array([0, 0, 1, 2, 3])
>>> pd.qcut(range(5), 3, labels=["good", "medium", "bad"])
...
[good, good, medium, bad, bad]
Categories (3, object): [good < medium < bad]
import pandas as pd
#设置切分区域
listBins = [0, 10, 20, 30, 40, 50, 60, 1000000]
#设置切分后对应标签
listLabels = ['0_10','11_20','21_30','31_40','41_50','51_60','61及以上']
#利用pd.cut进行数据离散化切分
"""
pandas.cut(x,bins,right=True,labels=None,retbins=False,precision=3,include_lowest=False)
x:需要切分的数据
bins:切分区域
right : 是否包含右端点默认True,包含
labels:对应标签,用标记来代替返回的bins,若不在该序列中,则返回NaN
retbins:是否返回间距bins
precision:精度
include_lowest:是否包含左端点,默认False,不包含
"""
df['fenzu'] = pd.cut(df['data'], bins=listBins, labels=listLabels, include_lowest=True)