立即学习:https://edu.csdn.net/course/play/26990/361136?utm_source=blogtoedu
数据离散化就是分箱
一般方法是等频或者等宽分箱
一般使用pd.cut或pd.qcut函数
pd.cut(x,bins,right = True,labels)
x:数据
bins:离散化的数目或者区间
labels:离散化后各个类别的标签
right:是否包含区间右边的值
df['Price_bin'] = pd.cut(df['Price_new'],5,labels = range(5))
df['Price_bin'].value_counts().plot(kind=bar)
#分类柱状图显示
或者,df['Price_bin'].hist()
w = [100,1000,5000,10000,20000,100000]
df['Price_bin'] = pd.cut(df['Price_new'],bins=w,labels=range(5))#自定义
等频分箱
k=5
w=[1.0*i/k for i in range(k+1)]
df["Price_bin"]=pd.qcut(df['Price_new',q=w,labels=range(5))