创建一列,将数字评级分类为"正"、"中性"和"负"



我正试图创建一列,根据数据帧中相邻列中的值将数字评级分类为正、中性、负。有什么想法吗?谢谢

#create Target Column "Positive, Neutral, Negative"
df['Target'] = df.StarsInt.map((5:'Positive',4:'Positive',3:'Neutral',2:'Negative',1:'Negative'))

使用np.select

conditions = [
df.StarsInt >= 4,
df.StarsInt == 3,
df.StarsInt <= 2,
]
values = ['Positive', 'Neutral', 'Negative']
df['Target'] = np.select(conditions, values)

使用pd.cut:

pd.cut(s, bins=[0,2,3,6], labels=['Negative', 'Nuetral', 'Positive'])

输出:

1    Negative
2    Negative
3     Nuetral
4    Positive
5    Positive
6    Positive

最新更新