过滤特定的记录和更早的使用窗口

发布于 2025-02-04 13:54:13 字数 954 浏览 3 评论 0原文

我有数据框,例如

name nimeatime
in2中的
a1
a3中的a 3中的
a3in
4inb
b41 in
inb 7in
b7 in b 7in
b18 in b 18in

我只想在我最后一次拥有<<代码>法规 =“ out”和之后的行。像这样:

名称时间法规
A4中的a 4
a5
B7out
b18in

I have the dataframe like

nametimestatut
A1in
A2out
A3in
A4out
A5in
B1in
B4in
B7out
B18in

I just want to get for each group the last time that I have statut = "out" and the row after. Like this:

nametimestatut
A4out
A5in
B7out
B18in

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

累赘 2025-02-11 13:54:13

这可以使用几个窗口功能完成。但是,使用窗口的功能不是很简单。

from pyspark.sql import functions as F, Window as W
df = spark.createDataFrame(
    [('A', 1, 'in'),
     ('A', 2, 'out'),
     ('A', 3, 'in'),
     ('A', 4, 'out'),
     ('A', 5, 'in'),
     ('B', 1, 'in'),
     ('B', 4, 'in'),
     ('B', 7, 'out'),
     ('B', 18, 'in'),
     ('B', 19, 'in'),
     ('C', 1, 'in')],
    ['name', 'time', 'statut']
)
w_max_out = W.partitionBy('name').orderBy(F.col('statut') != 'out', F.desc('time'))
w_lead = W.partitionBy('name').orderBy(F.desc('time'))
df = df.withColumn('_lead', F.lead('time').over(w_lead))
df = df.withColumn('_max_out', F.max(F.when(F.col('statut') == 'out', F.col('time'))).over(w_max_out))
df = df.filter('(_max_out = time) or (_max_out = _lead)').drop('_max_out', '_lead')

结果:

df.show()
# +----+----+------+
# |name|time|statut|
# +----+----+------+
# |   A|   4|   out|
# |   A|   5|    in|
# |   B|   7|   out|
# |   B|  18|    in|
# +----+----+------+

最后一个过滤行之前的结果:

# +----+----+------+-----+--------+
# |name|time|statut|_lead|_max_out|
# +----+----+------+-----+--------+
# |   A|   4|   out|    3|       4|
# |   A|   2|   out|    1|       4|
# |   A|   5|    in|    4|       4|
# |   A|   3|    in|    2|       4|
# |   A|   1|    in| null|       4|
# |   B|   7|   out|    4|       7|
# |   B|  19|    in|   18|       7|
# |   B|  18|    in|    7|       7|
# |   B|   4|    in|    1|       7|
# |   B|   1|    in| null|       7|
# |   C|   1|    in| null|    null|
# +----+----+------+-----+--------+

This can be done using a couple of window functions. However, the function which uses the window is not very simple.

from pyspark.sql import functions as F, Window as W
df = spark.createDataFrame(
    [('A', 1, 'in'),
     ('A', 2, 'out'),
     ('A', 3, 'in'),
     ('A', 4, 'out'),
     ('A', 5, 'in'),
     ('B', 1, 'in'),
     ('B', 4, 'in'),
     ('B', 7, 'out'),
     ('B', 18, 'in'),
     ('B', 19, 'in'),
     ('C', 1, 'in')],
    ['name', 'time', 'statut']
)
w_max_out = W.partitionBy('name').orderBy(F.col('statut') != 'out', F.desc('time'))
w_lead = W.partitionBy('name').orderBy(F.desc('time'))
df = df.withColumn('_lead', F.lead('time').over(w_lead))
df = df.withColumn('_max_out', F.max(F.when(F.col('statut') == 'out', F.col('time'))).over(w_max_out))
df = df.filter('(_max_out = time) or (_max_out = _lead)').drop('_max_out', '_lead')

Result:

df.show()
# +----+----+------+
# |name|time|statut|
# +----+----+------+
# |   A|   4|   out|
# |   A|   5|    in|
# |   B|   7|   out|
# |   B|  18|    in|
# +----+----+------+

Result before the last filter line:

# +----+----+------+-----+--------+
# |name|time|statut|_lead|_max_out|
# +----+----+------+-----+--------+
# |   A|   4|   out|    3|       4|
# |   A|   2|   out|    1|       4|
# |   A|   5|    in|    4|       4|
# |   A|   3|    in|    2|       4|
# |   A|   1|    in| null|       4|
# |   B|   7|   out|    4|       7|
# |   B|  19|    in|   18|       7|
# |   B|  18|    in|    7|       7|
# |   B|   4|    in|    1|       7|
# |   B|   1|    in| null|       7|
# |   C|   1|    in| null|    null|
# +----+----+------+-----+--------+
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文