【问题标题】:Is there a way to do a weighted ranking of 3 columns in pandas?有没有办法对熊猫中的 3 列进行加权排名?
【发布时间】:2020-04-16 14:54:05
【问题描述】:

我想根据某些权重对三列进行排名。这是我正在使用的数据框的示例:

Sales Revenue             Product Count           Average Sales/Product
1005650                   30                      33521.67
100223                    5                       20044.60
72233                     2                       36116.50
1005657                   13                      77358.23
1002233                   25                      40089.32

我想创建一个排名,例如,平均销售额/产品的权重为 45%,销售收入的权重为 35%,产品数量的权重为 20%。我知道在 pandas 中可以做到这一点:

col1 = sales["Sales Revenue"].astype(str)
col2 = sales["Average Sales/Product"].astype(str) 
col3 = sales["Product Count"].astype(str)

sales['Rank'] = (col2+col1+col3).astype('int64').rank(method='dense', ascending=False).astype(int64)
sales = sales.sort_values('Rank')

但我不明白这个方法是如何工作的,因为它确实有排名,但它背后的逻辑我似乎无法理解。有没有一种方法可以创建一个基于百分比权重的排名系统,从而能够创建一个排名,更加强调平均销售额/产品,然后是销售收入,然后是产品数量。

【问题讨论】:

    标签: python pandas rank


    【解决方案1】:

    您可以简单地计算加权平均值并对其进行排名:

    sales['Rank'] = (sales['Average Sales/Product'] * 45 +
            sales['Sales Revenue'] * 35 + sales['Product Count'] * 20).rank(
                ascending=False).astype('int64')
    

    【讨论】:

      猜你喜欢
      • 2020-10-21
      • 2020-03-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多