Home > other >  Pyspark how to implement the adjacent two rows of subtraction?
Pyspark how to implement the adjacent two rows of subtraction?

Time:09-20

Df. Show (6) as shown below.
+ -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- + + -- -- -- -- -- -- -- --
| MSID | etime |
+ -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- + + -- -- -- -- -- -- -- --
| 00 e91b511d5e | | 0
55 | | 00 e91b511d5e |
115 | | 00 e91b511d5e |
175 | | 00 e91b511d5e |
235 | | 00 e91b511d5e |
294 | | 00 e91b511d5e |

Etime before and after the subtraction, want to get the following results:
+ -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- + + -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- +
| MSID | etime | order |
+ -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- + + -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- +
55 | | 00 e91b511d5e | | 0
55 60 | | | 00 e91b511d5e |
| 00 e91b511d5e 60 | | 115 |
| 00 e91b511d5e 60 | | 175 |
| 00 e91b511d5e 60 | | 235 |
| 00 e91b511d5e | | | - 1 294

CodePudding user response:

Spark has a built-in methods lag and lead, on behalf of the meaning of please refer to the oracle, access to a record or a record; This method returns the column values to each record to generate a new column,
The column is the lag or lead the corresponding column, column operations that can be; Need to sort data in advance.
  • Related