I am new to AWS Glue, Python and PySparK. In my dataset, I am creating a Glue job with PySpark dataframe that will perform a concat distinct then group by while forming unique rows. I have 2 columns, 'project_id' and 'item'. I need to group by 'Project_ID, then show 'Item' values concatenated into the unique 'Project_ID' rows. Data sample:
Project_ID Item
10291318 204281
10291318 204284
10217060 106934
10217060 106934
10217060 106935
46439
47325
51862
10225836 118100
10225836 289841
10262244 163491
10262244 271251
10262244 271252
Expected result:
Project_ID Item
10291318 204281, 204284
10217060 106934, 106935
46439
47325
51862
10225836 118100, 289841
10262244 163491, 271251, 271252
Here is my dataframe, PySpark code:
def GroupRows(glueContext, dfc) -> DynamicFrameCollection:
df = dfc.select(list(dfc.keys())[0]).toDF()
from awsglue.dynamicframe import DynamicFrame
from pyspark.sql import functions as f
import datetime
df=df.groupby("Project ID")\
.agg(f.concat_ws(',',f.collect_set("Item")).alias("Item"))
return DynamicFrameCollection({"ct0":dfc}, glueContext)
I have looked up numerous examples but none seem to provide the expected result. Any help would be much appreciated.