###创建DatFrame数据集
import pandas as pd
import numpy as np
test=pd.DataFrame({'a':np.random.rand(100),
'b':np.random.rand(100)})
###
a b
0 0.911332 0.482893
1 0.011213 0.066976
2 0.171328 0.172378
3 0.964748 0.546019
4 0.710652 0.422444
5 0.029460 0.811418
6 0.841439 0.665418
7 0.844608 0.579587
8 0.251564 0.521302
9 0.010093 0.813377
10 0.935075 0.286420
###假设需要将’a'列的0,1之间区别为10份,并重新赋值为1到10
listbin=[0,0.1,0.2,0.3,0.4,0.5,0.6,0.7,0.8,0.9,1.0]
listlabels=[1,2,3,4,5,6,7,8,9,10]
test['a']=pd.cut(test['a'],bins=listbin,labels=listlabels,right=True)
###结果
a b
0 10 0.482893
1 1 0.066976
2 2 0.172378
3 10 0.546019
4 8 0.422444
5 1 0.811418
6 9 0.665418
7 9 0.579587
8 3 0.521302
9 1 0.813377
10 10 0.286420