【问题标题】:Proc rank alternative in PySparkPySpark 中的 Proc 等级替代方案
【发布时间】:2020-04-03 20:00:30
【问题描述】:

我想在 PySpark 中转换以下 SAS 代码:

SAS:

    proc rank data = INP(where = (col= 1)) 
              out = RESULT groups = 3 descending ;
       var Col1
           Col2
           Col3
           Col4;
       ranks R_Col1 F_Col2 M_Col3 O_Col4 ;
    run ;

我正在尝试使用下面的 PySpark 代码实现上述目标,但出现“DataFrame”对象没有属性“apply”的错误 PySpark:

def grouping(data):
    dec=pd.qcut(data['Col1','Col2','Col3','Col4'],3,labels=False)
    data['ranks']=dec
    return data
RESULT =INP.apply(grouping)

不胜感激!

谢谢

【问题讨论】:

  • 这不是 pyspark 代码。您正在使用来自 pandas 的 qcut(),pyspark 不支持它。也就是说,还有其他方法可以实现您想要的。查看 pyspark 函数文档以获取更多信息。你想通过 ntile 或在那里排名。

标签: pyspark sas proc


【解决方案1】:

尝试了以下解决方案:-

RESULT = sqlContext.sql(
"""
SELECT  *,
     ntile(3) OVER (order by Col1 desc) AS R_Col1,
     ntile(3) OVER (order by Col2 desc) AS F_Col2,
     ntile(3) OVER (order by Col3 desc) AS M_Col3,
     ntile(3) OVER (order by Col4 desc) AS O_Col4
FROM INP
WHERE col=1
"""
)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-08
    • 1970-01-01
    • 2012-11-02
    • 1970-01-01
    相关资源
    最近更新 更多