答案 0 :(得分:0)
您可以使用numpy.select
:
In [744]: df
Out[744]:
A
0 12
1 23
2 18
3 39
4 15
In [744]: import numpy as np
In [745]: conditions = [df.A.le(20), df.A.gt(20)]
In [746]: choices = ['Less than or equal to 20', 'Greater than 20']
In [749]: df['categorical_A'] = np.select(conditions, choices)
In [750]: df
Out[750]:
A categorical_A
0 12 Less than or equal to 20
1 23 Greater than 20
2 18 Less than or equal to 20
3 39 Greater than 20
4 15 Less than or equal to 20