python - 如何检查单元格中的值是否在范围内。基于此将不同的 'scores' 分配给新列

标签 python pandas numpy for-loop dataframe

我有这样的数据框:

orderID       Amount
   0          340.00
   1          200.00
   2           10.00
   3          500.00
   4          700.00

我想对每个订单金额进行“评分”,条件是金额在指定范围内或“超出”范围。

我的范围是:low = 300high = 500 如果在此范围内,我想指定分数 = 100。 如果 Amount 列中的值超出范围,我想应用如下评分函数:

if Amount > high:
    df['score'] = 100 - (Amount - high)/100
else:
    df['score'] = 100 - (low - Amount)/100

我尝试了这样的代码片段,但需要很长时间(数百万行):

 def pricing_function (df, column_name):
         for l in range(0,df.shape[0]):
             if (df[column_name].iloc[l] <= high and df[column_name].iloc[l] >= low):
                 df['score'][l] = 100
             elif df[column_name].iloc[l] > high:
                 df['score'][l] = 100 - (low - Amount)/100
             else:
                 df['score'][l] = 100 - (low - Amount)/100

然后将函数应用为:

df['score'] = df.apply(pricing_function(df= my_df, column_name = '金额'))

因此,我想要这样的数据框:

orderID      Amount    score
   0         340.00     100     
   1         200.00      99
   2          10.00      71
   3         500.00     100
   4         700.00      80

使用 for 循环迭代每个单元格需要很长时间,而且我在 StackOverflow 上找不到解决方案。 我尝试使用:

df['score'] = my_df['Amount']. Between(low, high, Included = True)

这给出了范围内/外的值的 True/False 值。我可以在最后一步中将 bool 值更改为 1/0 和 100/0,但在有效应用我的 rating_function 方面仍然存在问题。

最佳答案

numpy.where 功能:

df['score'] = np.where(df.Amount.between(low, high), 100,
                       np.where(df.Amount > high, 100 - (df.Amount - high)/100,
                                                  100 - (low - df.Amount)/100))

交互式:

In [46]: df['score'] = np.where(df.Amount.between(low, high), 100, np.where(df.Amount > high, 100 - (df.Amount - high)/100, 100 - (low - df.Amount)/100))

In [47]: df
Out[47]: 
   orderID  Amount  score
0        0   340.0  100.0
1        1   200.0   99.0
2        2    10.0   97.1
3        3   500.0  100.0
4        4   700.0   98.0

关于python - 如何检查单元格中的值是否在范围内。基于此将不同的 'scores' 分配给新列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51013059/

相关文章:

python - 无法使用pip语法错误安装PyAudio

python - 在python pandas中动态跳过excel的顶部空白行

python - 适用于许多变量的高效 python for 循环

python - 过滤标签与 taggit 中其他模型的关系

python - 为什么我被阻止使用 discord api?

python - 如何使用 Tkinter 上的入口小部件通过输入参数来执行函数?

python - 按对象对两个 Pandas 分组求和

python - 计算一对多数据帧中的时间差

python - 128x512 数组列表中的图像列表可提高效率

python - 什么是存储 NLP 嵌入的好方法(nparrays 加信息)