簡體   English   中英

pandas groupby.apply to pyspark

[英]pandas groupby.apply to pyspark

I have the following custom function to make aggregation in my pandas dataframe, and I want to do the same things in pyspark:

def custom_aggregation_pyspark(x,queries):
    names={}
    for k, v in regles_calcul.items():
        plus = x.query(v["plus_credit"])['OBNETCRE'].sum() + x.query(v["plus_debit"])['OBNETDEB'].sum()
        minus = x.query(v["minus_credit"])['OBNETCRE'].sum() + x.query(v["minus_debit"])['OBNETDEB'].sum()  

     

        names[k]= plus-minus
    return pd.Series(names, index=list(names.keys()))


df = df.groupby(['LBUDG']).apply(custom_aggregation_pandas, queries ).sum()

查詢是查詢的字典,例如

{'first_queries': {   
    'plus_credit': "classe_compte_rg2 in ('237', '238')",
    'plus_debit': "classe_compte_rg2 in ('237', '238')", 
    'minus_credit': "classe_compte_rg2 in ('237', '238')", 
    'minus_debit': "classe_compte_rg1 in ('20', '21', '23')"
     }
}

所以,我用 pyspark 'sql' 替換了 pandas “查詢”

def custom_aggregation_pyspark(x,queries):
    x.createOrReplaceTempView("df")
    names={}
    for k , v in queries.items():
        plus = spark.sql("SELECT * FROM df WHERE "+ v["plus_credit"]).select('OBNETCRE').groupby('OBNETCRE').sum().collect() + spark.sql("SELECT * FROM df WHERE "+ v["plus_debit"]).select('OBNETDEB').groupby('OBNETDEB').sum().collect() 
        minus= spark.sql("SELECT * FROM df WHERE "+ v["minus_credit"]).select('OBNETCRE').groupby('OBNETCRE').sum().collect() + spark.sql("SELECT * FROM df WHERE "+ v["minus_debit"]).select('OBNETDEB').groupby('OBNETDEB').sum().collect() 
        names[k]= plus-minus
    return pd.Series(names, index=list(names.keys()))

df.groupby("LBUDG").agg(custom_aggregation_pyspark(df,queries))

我肯定走錯了方向,因為上面的代碼不起作用,你能指導我應該看哪里嗎?

所需的 output 是按LBUDG (字符串)分組的表,其他列使用自定義聚合函數。

編輯 Dataframe 示例:

LBUDG 奧內特 OBNETDEB classe_compte_rg0 classe_compte_rg1
樂波薩 0,00 0,00 1 10
樂波薩 67572,00 0,00 1 10
樂波薩 0,00 0,00 1 10
樂波薩 4908,12 0,00 1 10
樂波薩 0,00 0,00 1 10
大四 295240,67 0,00 1 10
樂波薩 0,00 0,00 1 11
樂波薩 0,00 0,00 1 12
樂波薩 0,00 0,00 1 13
樂波薩 0,00 0,00 1 13
樂波薩 53697,94 0,00 1 13

預期 output:

LBUDG AGG1 AGG2
樂波薩 用於 LE POIZAT 的 agg1_value ...
大四 …… ...

其中 agg1 對應(例如),對應於OBNETCRE - OBNETDEB的總和,其中classe_compte_rg1具有值,是 10 或 11。

您可以使用epxr評估queries字典中傳遞的條件,並使用條件聚合來計算總和。 這是一個與您在 pandas 中給出的示例等效的示例:

from pyspark.sql import functions as F


def custom_aggregation_pyspark(df, regles_calcul):
    df1 = df.groupBy("LBUDG") \
        .agg(
        *[
            ((F.sum(F.when(F.expr(v["plus_credit"]), F.col("OBNETCRE")).otherwise(0)) +
              F.sum(F.when(F.expr(v["plus_debit"]), F.col("OBNETDEB")).otherwise(0))) -
             (F.sum(F.when(F.expr(v["minus_credit"]), F.col("OBNETCRE")).otherwise(0)) +
              F.sum(F.when(F.expr(v["minus_debit"]), F.col("OBNETDEB")).otherwise(0)))
             ).alias(k)

            for k, v in regles_calcul.items()
        ]
    )

    return df1


df = custom_aggregation_pyspark(df, queries)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM