问:

您好,可以请教下,如果0一个箱子,1一个箱子,2一个箱子,这种情况,代码怎么写呢

答:
比如你的数据中score5这一列就是你说的这种情况,这种情况不需要分箱,直接看每个值出现的频数就行。
用下面的命令。


score5各箱人数=data1["score5"].value_counts()
score5各箱人数.sort_index(inplace=True)

image.png




score5badcount=data1[data1["target"]==1]["score5"].value_counts()
score5goodcount=data1[data1["target"]==0]["score5"].value_counts()
#将几个指标按照索引进行横向合并
picdata5=pd.concat([score5各箱人数,score5badcount,score5goodcount],axis=1)
picdata5.columns=["score5各箱人数","score5badcount","score5goodcount"]
#计算各箱的bad%
picdata5["badper"]=picdata5["score5badcount"]/picdata5["score5各箱人数"]

image.png

import matplotlib.pyplot as plt

#解决中文乱码问题

plt.rcParams['font.sans-serif'] = ['Simhei']

fig,ax1=plt.subplots()

#[0,1,2]是横轴的刻度线标签

ax1.bar([0,1,2],picdata5["score5各箱人数"],label="坏人数",color='r',width=0.6)

ax1.bar([0,1,2],picdata5["score5goodcount"],label="好人数",color='b',width=0.6)


ax1.set_ylim([0,600]) # y轴边界


ax1.legend(loc=2)


ax2=ax1.twinx()

ax2.plot(picdata5["badper"],'r',label="bad百分比")

ax2.set_ylim([0,1]) # y轴边界

ax2.legend(loc=1)

fig.suptitle(u'score5的情况',fontsize=15)

image.png

0 0 0