简体   繁体   English

Pyspark数据框:获取符合条件的上一行

[英]Pyspark Dataframe: Get previous row that meets a condition

For every row in a PySpark DataFrame I am trying to get a value from the first preceding row that satisfied a certain condition: 对于PySpark DataFrame中的每一行,我都试图从前一个满足特定条件的第一行中获取一个值:

That is if my dataframe looks like this: 那就是我的数据框看起来像这样:

X  | Flag
1  | 1
2  | 0
3  | 0
4  | 0
5  | 1
6  | 0
7  | 0
8  | 0
9  | 1
10 | 0

I want output that looks like this: 我想要看起来像这样的输出:

X  | Lag_X | Flag
1  | NULL  | 1
2  | 1     | 0
3  | 1     | 0
4  | 1     | 0
5  | 1     | 1
6  | 5     | 0
7  | 5     | 0
8  | 5     | 0
9  | 5     | 1
10 | 9     | 0

I thought I could do this with lag function and a WindowSpec, unfortunately WindowSpec doesnt support .filter or .when , so this does not work: 我以为我可以用延迟功能和WindowSpec做到这一点,可惜WindowSpec犯规支持.filter.when ,所以这不工作:

conditional_window = Window().orderBy(X).filter(df[Flag] == 1)
df = df.withColumn('lag_x', f.lag(df[x],1).over(conditional_window)

It seems like this should be simple, but I have been racking my brain trying to find a solution so any help with this would be greatly appreciated 看起来这应该很简单,但是我一直在努力寻找解决方案,因此对此提供的任何帮助将不胜感激

Question is old, but I thought the answer might help others 问题很老,但我认为答案可能会对其他人有所帮助

Here is a working solution using window and lag functions 这是使用窗口和滞后功能的有效解决方案

from pyspark.sql import functions as F
from pyspark.sql import Window
from pyspark.sql.functions import when
from pyspark.context import SparkContext

# Call SparkContext
sc = SparkContext.getOrCreate()
sc = sparkContext

# Create DataFrame
a = sc.createDataFrame([(1, 1), 
                        (2, 0),
                        (3, 0),
                        (4, 0),
                        (5, 1),
                        (6, 0),
                        (7, 0),
                        (8, 0),
                        (9, 1),
                       (10, 0)]
                     , ['X', 'Flag'])

# Use a window function
win = Window.orderBy("X")
# Condition : if preceeding row in column "Flag" is not 0
condition = F.lag(F.col("Flag"), 1).over(win) != 0
# Add a new column : if condition is true, value is value of column "X" at the previous row
a = a.withColumn("Flag_X", F.when(condition, F.col("X") - 1))

Now, we obtain a DataFrame as shown below 现在,我们获得一个DataFrame,如下所示

+---+----+------+
|  X|Flag|Flag_X|
+---+----+------+
|  1|   1|  null|
|  2|   0|     1|
|  3|   0|  null|
|  4|   0|  null|
|  5|   1|  null|
|  6|   0|     5|
|  7|   0|  null|
|  8|   0|  null|
|  9|   1|  null|
| 10|   0|     9|
+---+----+------+

To fill null values : 要填充空值:

a = a.withColumn("Flag_X", 
                 F.last(F.col("Flag_X"), ignorenulls=True)\
     .over(win))

So the final DataFrame is as required : 所以最终的DataFrame是按要求的:

+---+----+------+
|  X|Flag|Flag_X|
+---+----+------+
|  1|   1|  null|
|  2|   0|     1|
|  3|   0|     1|
|  4|   0|     1|
|  5|   1|     1|
|  6|   0|     5|
|  7|   0|     5|
|  8|   0|     5|
|  9|   1|     5|
| 10|   0|     9|
+---+----+------+

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

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