[Python] How can I group these related rows using PySpark?

Discussão em 'Python' iniciado por Stack, Dezembro 19, 2024.

  1. Stack

    Stack Membro Participativo

    I need to parse some text data in Python PySpark on Databricks. The data look like this:

    df = spark.createDataFrame([("new entry", 1, 123),
    ("acct", 2, None),
    ("cust ID", 3, None),
    ("new entry", 4, 456),
    ("acct", 5, None),
    ("more text", 6, None),
    ("cust ID", 7, None)],
    ("value", "line num", "tracking ID"))


    Here I manually added the "need grouping" column to illustrate - rows from "new entry" to "cust ID" are one group, followed by another. They are not all the same length.

    [​IMG]

    I need to match up cust ID with the tracking ID a few lines before, so something like this:

    [​IMG]

    How can I match cust ID with tracking ID? I thought of a window function but I'm not sure how to create the needed grouping.

    Continue reading...

Compartilhe esta Página