ComenzarEmpieza gratis

SQL de agregación con notación por puntos

El siguiente código usa SQL para asignar el valor de un DataFrame llamado df.

df = spark.sql("""
SELECT *, 
LEAD(time,1) OVER(PARTITION BY train_id ORDER BY time) AS time_next 
FROM schedule
""")
  • La cláusula LEAD tiene una función equivalente en pyspark.sql.functions.
  • Las cláusulas PARTITION BY y ORDER BY tienen cada una una función equivalente en notación por puntos que se invoca sobre el objeto Window.
  • Están disponibles las siguientes importaciones:
    • from pyspark.sql import Window
    • from pyspark.sql.functions import lead

Este ejercicio forma parte del curso

Introducción a Spark SQL en Python

Ver curso

Instrucciones del ejercicio

  • Crea un DataFrame llamado dot_df que contenga el mismo resultado que df, usando notación por puntos en lugar de SQL.

Ejercicio interactivo práctico

Prueba este ejercicio y completa el código de muestra.

# Obtain the identical result using dot notation 
dot_df = df.withColumn('time_next', ____('time', 1)
        .over(____.____('train_id')
        .____('time')))
Editar y ejecutar código