简体   繁体   English

数据框pyspark更新前一行的行

[英]dataframe pyspark update rows from previous rows

I am using pyspark, and I have a dataframe that looks like that :我正在使用 pyspark,并且我有一个如下所示的数据框:

CODE  |  POSITION|  COL1 | COL2
A     |  1       |       | 
A     |  2       |       | AAA
A     |  3       |   INF |
A     |  4       |   BIC |
A     |  5       |       |
B     |  1       |       | BBB
B     |  2       |   MIL |
B     |  3       |       |
B     |  4       |       | CCC
B     |  5       |       |
B     |  6       |       |

and I want to have that :我想要那个:

CODE  |  POSITION|  COL1 | COL2
A     |  1       |       | 
A     |  2       |       | AAA
A     |  3       |   INF | AAA
A     |  4       |   BIC | AAA
A     |  5       |       |
B     |  1       |       | BBB
B     |  2       |   MIL | BBB
B     |  3       |       |
B     |  4       |       | CCC
B     |  5       |       |
B     |  6       |       |

I explain, this dataframe is grouped by "CODE" and ordered by "POSITION", I need for a group "CODE" , when I have "COL2" filled (position =2 in this example) to take the value "AAA" and put it in the following positions 3 and 4 (while COL1 is filled)我解释一下,这个数据框按“CODE”分组并按“POSITION”排序,我需要一个组“CODE”,当我填充“COL2”(在本例中位置=2)取值“AAA”和将其放在以下位置 3 和 4(同时填充 COL1)

I know is not that easy(for me!)我知道没那么容易(对我来说!)

Thank you a lot for your help非常感谢您的帮助

It can be done using the last function.可以使用last函数来完成。
F.last returns the last non-null value in an ordered window. F.last返回有序窗口中的最后一个非空值。

Your dataframe:您的数据框:

from pyspark.sql.functions import col
from pyspark.sql.functions import lag
from pyspark.sql.window import Window
from pyspark.sql import functions as F
import sys

df = sc.parallelize([['A', 1, None, None], ['A', 2, None, 'AAA'], ['A', 3, 'INF', None], ['A', 4, 'BIC', None], ['A', 5, None, None], ['B', 1, None, 'BBB'], ['B', 2, 'MIL', None], ['B', 3, None, None], ['B', 4, None, 'CCC'], ['B', 5, None, None], ['B', 6, None, None]])
df = df.toDF(['code', 'position', 'col1', 'col2'])
w = Window.partitionBy("code").orderBy("position")

df.withColumn("col3", F.last('col2', True).over(w.rowsBetween(-sys.maxsize, 0)))\
    .withColumn("col3", F.when(col("col1").isNull(), col("col2"))
    .otherwise(col("col3")))\
    .drop("col2").withColumnRenamed("col3", "col2")\
    .orderBy("code", "position").show()

Output:输出:

+----+--------+----+----+                                                       
|code|position|col1|col2|
+----+--------+----+----+
|   A|       1|null|null|
|   A|       2|null| AAA|
|   A|       3| INF| AAA|
|   A|       4| BIC| AAA|
|   A|       5|null|null|
|   B|       1|null| BBB|
|   B|       2| MIL| BBB|
|   B|       3|null|null|
|   B|       4|null| CCC|
|   B|       5|null|null|
|   B|       6|null|null|
+----+--------+----+----+

If col1 corresponding to position 6 is filled, it will return CCC in col2 .如果position 6对应的col1被填满,则在col2返回CCC
It takes the latest non-null value in col2 as it progresses through the window.当它在窗口中前进时,它采用col2最新的非空值。

+----+--------+----+----+
|   B|       6| XYZ| CCC|
+----+--------+----+----+

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

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