from pyspark.sql.types import * import pyspark.sql.functions as F schema = StructType([ StructField('name', StringType(), nullable=False), StructField("business", ArrayType(StringType())), StructField('total', IntegerType(), nullable=False) ]) test_list = ['a',['Hello', 'world'],200], ['a',['I', 'am', 'fine'],300],['b',[],500] df = spark.createDataFrame(test_list,schema=schema) df1=df.groupBy('name','business').agg(F.when(F.size('business')>1,F.sum('total')/F.size('business')).otherwise(F.sum('total')).alias('total')) df1 = df1.withColumn('business',F.explode_outer('business'))