apache-spark - Spark-Python : Select rows and dates

标签 apache-spark date select pyspark count

我在 Spark (Python) 中有以下 df。 我只是想选择“datos_acumulados”列超过 20480 的那一天。在这种情况下,输出应该是如下所示的表格:(包含空值的表格格式):

结果:

 grupo_edad|     fecha|acumuladosMB|datos_acumulados|
|         1|2020-08-04|        4864|           20921|
|         4|      null|        null|            null|

数据框:df_datos_acumulados

     grupo_edad|     fecha|acumuladosMB|datos_acumulados|
    +----------+----------+------------+----------------+
    |         1|2020-08-01|        6185|            6185|
    |         1|2020-08-02|        5854|           12039|
    |         1|2020-08-03|        4018|           16057|
    |         1|2020-08-04|        4864|           20921|
    |         1|2020-08-05|        5526|           26447|
    |         1|2020-08-06|        4818|           31265|
    |         1|2020-08-07|        5359|           36624|
    |         4|2020-08-01|         674|             674|
    |         4|2020-08-02|         744|            1418|
    |         4|2020-08-03|         490|            1908|
    |         4|2020-08-04|         355|            2263|
    |         4|2020-08-05|        1061|            3324|
    |         4|2020-08-06|         752|            4076|
    |         4|2020-08-07|         560|            4636|

谢谢!

感谢 @pasha701 的回答,我可以获得最终表,但它没有显示我也需要的空行:

grupoDistinctDF = df_datos_acumulados.withColumn("grupo_edad", col("grupo_edad"))


grupoWindow = Window.partitionBy("grupo_edad").orderBy("fecha")

df_datos_acumulados = df_datos_acumulados.where(col("datos_acumulados") >= 20480) \
  .withColumn("row_number", row_number().over(grupoWindow)) \
  .where(col("row_number") == 1) \
  .drop("row_number")


grupoDistinctDF = grupoDistinctDF.join(df_datos_acumulados,["grupo_edad"], "left")

输出:

 grupo_edad|     fecha|acumuladosMB|datos_acumulados|
|         1|2020-08-04|        4864|           20921|

最佳答案

如果需要“datos_acumulados”> 20480 的第一行,可以使用窗口函数“row_number()”来获取这样的第一行,并与不同的“grupo_edad”(Scala)连接:

val df = Seq(
  (1, "2020-08-01", 6185, 6185),
  (1, "2020-08-02", 5854, 12039),
  (1, "2020-08-03", 4018, 16057),
  (1, "2020-08-04", 4864, 20921),
  (1, "2020-08-05", 5526, 26447),
  (1, "2020-08-06", 4818, 31265),
  (1, "2020-08-07", 5359, 36624),
  (4, "2020-08-01", 674, 674),
  (4, "2020-08-02", 744, 1418),
  (4, "2020-08-03", 490, 1908),
  (4, "2020-08-04", 355, 2263),
  (4, "2020-08-05", 1061, 3324),
  (4, "2020-08-06", 752, 4076),
  (4, "2020-08-07", 560, 4636)
).toDF("grupo_edad", "fecha", "acumuladosMB", "datos_acumulados")

val grupoDistinctDF = df.select("grupo_edad").distinct()

val grupoWindow = Window.partitionBy("grupo_edad").orderBy("fecha")

val firstMatchingRowDF = df
  .where($"datos_acumulados" > 20480)
  .withColumn("row_number", row_number().over(grupoWindow))
  .where($"row_number" === 1)
  .drop("row_number")


grupoDistinctDF.join(firstMatchingRowDF, Seq("grupo_edad"), "left")

输出:

+----------+----------+------------+----------------+
|grupo_edad|fecha     |acumuladosMB|datos_acumulados|
+----------+----------+------------+----------------+
|4         |null      |null        |null            |
|1         |2020-08-04|4864        |20921           |
+----------+----------+------------+----------------+

关于apache-spark - Spark-Python : Select rows and dates,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69193818/

相关文章:

apache-spark - 尝试从Spark查询HBase上的数据时出现NoSuchMethodError

java - 根据另一个 RDD 的值聚合一个 RDD Spark (Java)

php - 如何从一个mysql表的日期时间字段中仅获取日期,合并相似的日期并通过cron作业更新另一个表

MySQL 列出所有重复项

PHP 未正确回显下拉菜单的值

apache-spark - Spark : writing data to place that is being read from without loosing data

apache-spark - translate 和 regexp_replace 的区别

python - 在给定各种条件的情况下,如何更改 pandas 数据框中日期的年份?

sql - 比较 SQL Server 中两个日期范围之间的数据

sql - 如何按 A 列排序,然后按 B 列排序?