且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在Pandas DataFrame中对一系列值进行分类

更新时间:2022-12-29 19:41:18

对我来说,

For me working cat.codes with indexing by converting list a to numpy array:

a = list('ABCDEF')
df['new'] = np.array(a)[pd.cut(df["Area"], bins = bins).cat.codes]
print (df)
     Area new
0   14.68   B
1   40.54   C
2   10.82   A
3    2.31   A
4   22.30   C
5  600.00   F


catDf = pd.Series(np.array(a)[pd.cut(df["Area"], bins = bins).cat.codes], index=df.index)
print (catDf)
0    B
1    C
2    A
3    A
4    C
5    F
dtype: object