2

I have a dataframe as shown below. I have to take the rank of the row that has claim_typ as 'PD' and do a difference with the rank of of the current row and add it as a new column.

Source dataframe:

Id svc_dt clm_typ rank 48115882 20180209 RV 1 48115882 20180209 RJ 2 48115882 20180216 RJ 3 48115882 20180302 RJ 4 48115882 20180402 PD 5 48115882 20180502 RJ 6

Expected resultant dataframe:

Id svc_dt clm_typ rank diff_PD_Rank 48115882 20180209 RV 1 4 (Current rank - rank of column with 'PD') 48115882 20180209 RJ 2 3 48115882 20180216 RJ 3 2 48115882 20180302 RJ 4 1 48115882 20180402 PD 5 null 48115882 20180502 RJ 6 null

Premkumar
  • 29
  • 6

1 Answers1

1

PySpark solution.

Assuming there is one row per clm_type 'PD' per id, you can use conditional aggregation with max(when...)) to get the necessary difference.

# necessary imports 
w1 = Window.partitionBy(df.id).orderBy(df.svc_dt)
df = df.withColumn('rnum',row_number().over(w1))
w2 = Window.partitionBy(df.id)
res = df.withColumn('diff_pd_rank',max(when(df.clm_typ == 'PD',df.rnum)).over(w2) - rnum)
res.show()
Vamsi Prabhala
  • 48,685
  • 4
  • 36
  • 58