你可以使用cut:
bins = [-np.inf, .2, .4, .6, .8, 1]
labels = ["{0} - {1}".format(i, i + 20) for i in range(0, 100, 20)]
#same as
#labels=['0-20','20-40','40-60','60-80','80-100']
df['label'] = pd.cut(df['Probability'], bins=bins, labels=labels)
示例:
np.random.seed(100)
df = pd.DataFrame(np.random.random((10,1)), columns=['Probability'])
df.loc[0, 'Probability'] = 0
df.loc[8, 'Probability'] = 0.4
df.loc[9, 'Probability'] = 1
bins = [-np.inf, .2, .4, .6, .8, 1]
labels = ["{0} - {1}".format(i, i + 20) for i in range(0, 100, 20)]
df['label'] = pd.cut(df['Probability'], bins=bins, labels=labels)
print (df)
Probability label
0 0.000000 0-20
1 0.278369 20-40
2 0.424518 40-60
3 0.844776 80-100
4 0.004719 0-20
5 0.121569 0-20
6 0.670749 60-80
7 0.825853 80-100
8 0.400000 20-40
9 1.000000 80-100