Python 使用IF/ELSE为每行指定一个离散值

Python 使用IF/ELSE为每行指定一个离散值,python,Python,我有一个为网站用户评分的Python项目,输出是一个介于0和1之间的浮点数。例如: User score 0 123 0.04355 1 456 0.01074 2 789 0.00000 3 987 0.00000 4 654 0.00000 我想在我的数据中添加第二列,并使用诸如“High”、“Middle”和“Low”之类的谨慎标注——在Python中最好的方法是什么 到目前为止,我已经在IF语句中尝试

我有一个为网站用户评分的Python项目,输出是一个介于0和1之间的浮点数。例如:

   User      score
0  123       0.04355
1  456       0.01074
2  789       0.00000
3  987       0.00000
4  654       0.00000
我想在我的数据中添加第二列,并使用诸如“High”、“Middle”和“Low”之类的谨慎标注——在Python中最好的方法是什么

到目前为止,我已经在
IF
语句中尝试过,但它不起作用,但希望能说明我正在尝试做什么:

if logreg_results.propensity >= 0.90:
    logreg_results.band = "High"
elif logreg_results.propensity < 0.90 >= 0.70:
    logreg_results.band = "Good"
elif logreg_results.propensity <0.70 >= 0.50:
    logreg_results.band = "fair"

这将解决问题:

if logreg_results.propensity >= 0.90:
    logreg_results.band = "High"
elif logreg_results.propensity >= 0.70:
    logreg_results.band = "Good"
elif logreg_results.propensity >= 0.5:
    logreg_results.band = "fair"
如果第一个子句失败,它肯定不能高于
0.9
,依此类推。

与参数
right=False一起使用,以包括最右边的边:

bins = [-np.inf, .5, .7, .9, np.inf]
labels = ['Low', 'Fair', 'Good', 'High']
df['Banding'] = pd.cut(df['score'], bins=bins, labels=labels, right=False)
print (df)

   User   score Banding
0   123  0.7355    Good
1   456  0.6074    Fair
2   789  0.9000    High
3   987  0.5000    Fair
4   654  0.0000     Low
bins = [-np.inf, .5, .7, .9, np.inf]
labels = ['Low', 'Fair', 'Good', 'High']
df['Banding'] = pd.cut(df['score'], bins=bins, labels=labels, right=False)
print (df)

   User   score Banding
0   123  0.7355    Good
1   456  0.6074    Fair
2   789  0.9000    High
3   987  0.5000    Fair
4   654  0.0000     Low