简体   繁体   English

如何在 Dataframe、Pyspark 中更新具有多个条件的行

[英]How to update rows with many conditions in Dataframe, Pyspark

How to update rows in DataFrame(Pyspark, not scala) where the update should happen on certain conditions?如何更新 DataFrame(Pyspark,而不是 scala)中的行,在某些条件下应该发生更新? We dont know how many conditions will there be nor what they are during design time, so the conditions and the update values are to be applied at runtime.我们不知道在设计时会有多少条件,也不知道它们是什么,因此条件和更新值将在运行时应用。 Sample DataFrame.示例数据帧。

Table T1:表 T1:

| Emp_LName | Emp_FName     |  Sal          |   Sal_Grade  | 
| -------- | -------------- |-------------- |------------- |
| Smith    | Bob            |100000         |B             |
| Barnes   | Jim            |90000          |B             |
| Rogers   | Eric           |120000         |A             |
| Carson   | Ben            |45000          |C             |
df_source = spark.sql("Select * from T1)

lst_Conditions =[(Sal= 45000,Sal_Grade=E),(E_Name='Bob',E_Name='Robert),(One more candition),...].

(Basically the conditions are to be interpreted as 'Where Sal=45000, Then set Sal_Grade=E). (基本上,条件将被解释为'Where Sal=45000,然后设置Sal_Grade=E)。

How do I write code for each condition to update the dataframe?如何为每个条件编写代码来更新数据帧? Any help is appreciated.任何帮助表示赞赏。

Thank you R谢谢你

df_source = spark.createDataFrame(
  [
     ('Smith','Bob',100000,'B')
    ,('Barnes','Jim',90000,'B')
    ,('Rogers','Eric',120000,'A')
    ,('Carson','Ben',45000,'C')
  ], ['Emp_LName','Emp_FName','Sal','Sal_Grade']
)
                                           
lst_Conditions = [
    ('Cond_1', 'CASE WHEN Sal = 45000 THEN "E" END'),
    ('Cond_2', 'CASE WHEN Emp_FName = "Bob" THEN "V" END')
]

from pyspark.sql import functions as F

for c in lst_Conditions:
  df_source = df_source.withColumn(c[0], F.expr(c[1]))
  
df_source.show()

+---------+---------+------+---------+------+------+
|Emp_LName|Emp_FName|   Sal|Sal_Grade|Cond_1|Cond_2|
+---------+---------+------+---------+------+------+
|    Smith|      Bob|100000|        B|  null|     V|
|   Barnes|      Jim| 90000|        B|  null|  null|
|   Rogers|     Eric|120000|        A|  null|  null|
|   Carson|      Ben| 45000|        C|     E|  null|
+---------+---------+------+---------+------+------+

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM