I have a PySpark data Fram with 3 columns. Some rows are similar in 2 columns but not the third one, see below example.
----------------------------------------
first_name | last_name | requests_ID |
----------------------------------------
Joe | Smith |[2,3] |
----------------------------------------
Joe | Smith |[2,3,5,6] |
----------------------------------------
Jim | Bush |[9,7] |
----------------------------------------
Jim | Bush |[21] |
----------------------------------------
Sarah | Wood |[2,3] |
----------------------------------------
I want to group-by the rows based on {first_name, last_name} columns and only have the row with the maximum number of {requests_ID}. So the results should be:
----------------------------------------
first_name | last_name | requests_ID |
----------------------------------------
Joe | Smith |[2,3,5,6] |
----------------------------------------
Jim | Bush |[9,7] |
----------------------------------------
Sarah | Wood |[2,3] |
----------------------------------------
I have tries different things like the following, but it gives me a nested array of both rows in the group-by and not the longest one.
gr_df = filtered_df.groupBy("first_name", "last_name").agg(F.collect_set("requests_ID").alias("requests_ID"))
Here is the results I get:
----------------------------------------
first_name | last_name | requests_ID |
----------------------------------------
Joe | Smith |[[9,7],[2,3,5,6]]|
----------------------------------------
Jim | Bush |[[9,7],[21]] |
----------------------------------------
Sarah | Wood |[2,3] |
----------------------------------------