Pyspark基于新条件创建新的分类列

2024-09-28 17:25:02 发布

您现在位置:Python中文网/ 问答频道 /正文

我的输入火花数据帧是

    Year  Month        Client  Value 
    2019  1            1       5 
    2019  2            1       3  
    2019  3            1       4  
    2019  4            1       0  
    2019  5            1       3  
    2019  6            1       15 
    2019  7            1       45 
    2019  8            1       50 
    2019  9            1       64  
    2019  10           1       91   
    2019  11           1       1 
    2019  12           1       34  
    2019  1            2       0 
    2019  2            2       0  
    2019  3            2       0  
    2019  4            2       0  
    2019  5            2       1  
    2019  6            2       2 
    2019  7            2       10 
    2019  8            2       1 
    2019  9            2       4  
    2019  10           2       7 
    2019  11           2       1 
    2019  12           2       34  

Dataframe按客户、年份和月份进行订购

如果过去6个月(2019-06年至2019-12年)的最大值大于90,“目标”列为3

如果过去6个月(2019-06年至2019-12年)的最大值大于15且小于90,“目标”列为2

如果过去6个月(2019-06年至2019-12年)的最大值小于15,则“目标”列为1

我根据上述数据共享了所需的输出

    Year  Month        Client  Value Target
    2019  1            1       5     3
    2019  2            1       3     3
    2019  3            1       4     3
    2019  4            1       0     3 
    2019  5            1       3     3
    2019  6            1       15    3
    2019  7            1       45    3 
    2019  8            1       50    3
    2019  9            1       64    3 
    2019  10           1       91    3
    2019  11           1       1     3
    2019  12           1       34    3
    2019  1            2       0     2
    2019  2            2       0     2
    2019  3            2       0     2
    2019  4            2       0     2
    2019  5            2       1     2
    2019  6            2       2     2
    2019  7            2       10    2
    2019  8            2       1     2
    2019  9            2       4     2
    2019  10           2       7     2
    2019  11           2       1     2
    2019  12           2       34    2

你能帮我做这个吗


Tags: 数据clienttarget目标dataframe客户valueyear
2条回答

您可以筛选出6月份之前的所有月份,并为每个客户获得最大价值:

(
    df.filter(F.col('Month') >= 6)
    .groupby('Client')
    .agg(F.when(F.max('Value') > 90, 3)
         .when(F.max('Value') > 15, 2)
         .otherwise(1)
         .alias('Target'))
    .join(df, 'Client')
    .select(*df.columns, 'Target')
).show()

输出:

+  +  -+   +  -+   +
|Year|Month|Client|Value|Target|
+  +  -+   +  -+   +
|2019|    1|     1|    5|     3|
|2019|    2|     1|    3|     3|
|2019|    3|     1|    4|     3|
|2019|    4|     1|    0|     3|
|2019|    5|     1|    3|     3|
|2019|    6|     1|   15|     3|
|2019|    7|     1|   45|     3|
|2019|    8|     1|   50|     3|
|2019|    9|     1|   64|     3|
|2019|   10|     1|   91|     3|
|2019|   11|     1|    1|     3|
|2019|   12|     1|   34|     3|
|2019|    1|     2|    0|     2|
|2019|    2|     2|    0|     2|
|2019|    3|     2|    0|     2|
|2019|    4|     2|    0|     2|
|2019|    5|     2|    1|     2|
|2019|    6|     2|    2|     2|
|2019|    7|     2|   10|     2|
|2019|    8|     2|    1|     2|
+  +  -+   +  -+   +
only showing top 20 rows

您可以使用row_number筛选相关行,获得最大值,从而获得目标,并将其连接回原始数据帧

from pyspark.sql import functions as F, Window

df2 = df.withColumn(
    'rn', 
    F.row_number().over(Window.partitionBy('Client').orderBy(F.desc('year'), F.desc('month')))
).filter('rn <= 7').drop('rn').groupBy('Client').agg(
    F.when(F.max('Value') > 90, 3)
     .when(F.max('Value') > 15, 2)
     .otherwise(1)
     .alias('Target')
).join(df, 'Client').select(*df.columns, 'Target')

df2.show(99)
+  +  -+   +  -+   +
|Year|Month|Client|Value|Target|
+  +  -+   +  -+   +
|2019|    1|     1|    5|     3|
|2019|    2|     1|    3|     3|
|2019|    3|     1|    4|     3|
|2019|    4|     1|    0|     3|
|2019|    5|     1|    3|     3|
|2019|    6|     1|   15|     3|
|2019|    7|     1|   45|     3|
|2019|    8|     1|   50|     3|
|2019|    9|     1|   64|     3|
|2019|   10|     1|   91|     3|
|2019|   11|     1|    1|     3|
|2019|   12|     1|   34|     3|
|2019|    1|     2|    0|     2|
|2019|    2|     2|    0|     2|
|2019|    3|     2|    0|     2|
|2019|    4|     2|    0|     2|
|2019|    5|     2|    1|     2|
|2019|    6|     2|    2|     2|
|2019|    7|     2|   10|     2|
|2019|    8|     2|    1|     2|
|2019|    9|     2|    4|     2|
|2019|   10|     2|    7|     2|
|2019|   11|     2|    1|     2|
|2019|   12|     2|   34|     2|
+  +  -+   +  -+   +

相关问题 更多 >