如何选择最后一行以及如何按索引访问PySpark数据帧?

如何选择最后一行以及如何按索引访问PySpark数据帧?,第1张

如何选择最后一行以及如何按索引访问PySpark数据帧?

如何获得最后一行。

假设所有列都可修改的漫长而丑陋的方式:

from pyspark.sql.functions import (    col, max as max_, struct, monotonically_increasing_id)last_row = (df    .withColumn("_id", monotonically_increasing_id())    .select(max(struct("_id", *df.columns))    .alias("tmp")).select(col("tmp.*"))    .drop("_id"))

如果不是所有列都可以订购,则可以尝试:

with_id = df.withColumn("_id", monotonically_increasing_id())i = with_id.select(max_("_id")).first()[0]with_id.where(col("_id") == i).drop("_id")

注意。/

oassql.functions中有
last
函数,
pyspark.sql.functions`但考虑到对相应表达式的描述,此处不是一个好的选择。

我如何通过index.like访问数据框行



欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/zaji/5647393.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-16
下一篇 2022-12-16

发表评论

登录后才能评论

评论列表(0条)

保存