pyspark 在选定的行之后立即获取行
pyspark get the row immediately after the one selected
我有一个这样的数据框 (df):
col1
col2
col3
One
Two
x
One
Two
full
One
Two
y
One
Two
z
One
Two
full
One
Two
u
One
Two
e
使用 PySPark,我想在 col3=="full" 之后立即用 1 标记 element/rows,否则为 0,如下所示:
col1
col2
col3
flag
One
Two
x
0
One
Two
full
0
One
Two
y
1
One
Two
z
0
One
Two
full
0
One
Two
u
1
One
Two
e
0
目前这是我的想法,但我不会在...之后立即采取行动:
df.withColumn('flag',f.when(f.col('CD_OPERAZIONE')=='full',1).otherwise(0))
你能帮帮我吗?
第 1 步:使用 row_number 函数为每一行分配行号
第 2 步:使用 col3==full 过滤数据框,现在您有了 col3 已满的行号,称它为 dataframe2 让我们说
第 3 步:创建一个新列,将 1 添加到 dataframe2 中的行号列,现在您将在 col3 为 full 的行旁边拥有直接行的行号
第 4 步:在从 dataframe1 row_number 上的 dataframe2 和 dataframe2.
上的新行号列中选择新列后,通过内部连接将数据帧 1 与 dataframe2 连接起来
请原谅我的手机上没有代码。如果您还需要帮助,请告诉我。
使用lag和when语句
w= Window.partitionBy('col1','col2').orderBy('col1')
df.withColumn('x', when(lag('col3').over(w)=='full',1).otherwise(0)).show()
+----+----+----+---+
|col1|col2|col3| x|
+----+----+----+---+
| One| Two| x| 0|
| One| Two|full| 0|
| One| Two| y| 1|
| One| Two| z| 0|
| One| Two|full| 0|
| One| Two| u| 1|
| One| Two| e| 0|
+----+----+----+---+
我有一个这样的数据框 (df):
col1 | col2 | col3 |
---|---|---|
One | Two | x |
One | Two | full |
One | Two | y |
One | Two | z |
One | Two | full |
One | Two | u |
One | Two | e |
使用 PySPark,我想在 col3=="full" 之后立即用 1 标记 element/rows,否则为 0,如下所示:
col1 | col2 | col3 | flag |
---|---|---|---|
One | Two | x | 0 |
One | Two | full | 0 |
One | Two | y | 1 |
One | Two | z | 0 |
One | Two | full | 0 |
One | Two | u | 1 |
One | Two | e | 0 |
目前这是我的想法,但我不会在...之后立即采取行动:
df.withColumn('flag',f.when(f.col('CD_OPERAZIONE')=='full',1).otherwise(0))
你能帮帮我吗?
第 1 步:使用 row_number 函数为每一行分配行号 第 2 步:使用 col3==full 过滤数据框,现在您有了 col3 已满的行号,称它为 dataframe2 让我们说 第 3 步:创建一个新列,将 1 添加到 dataframe2 中的行号列,现在您将在 col3 为 full 的行旁边拥有直接行的行号 第 4 步:在从 dataframe1 row_number 上的 dataframe2 和 dataframe2.
上的新行号列中选择新列后,通过内部连接将数据帧 1 与 dataframe2 连接起来请原谅我的手机上没有代码。如果您还需要帮助,请告诉我。
使用lag和when语句
w= Window.partitionBy('col1','col2').orderBy('col1')
df.withColumn('x', when(lag('col3').over(w)=='full',1).otherwise(0)).show()
+----+----+----+---+
|col1|col2|col3| x|
+----+----+----+---+
| One| Two| x| 0|
| One| Two|full| 0|
| One| Two| y| 1|
| One| Two| z| 0|
| One| Two|full| 0|
| One| Two| u| 1|
| One| Two| e| 0|
+----+----+----+---+