Say I have data like the following:
from pyspark.sql import SparkSession
from pyspark.sql.types import ArrayType, DoubleType, StructField, StructType, LongType
spark = SparkSession.builder.appName("AveragingArray").getOrCreate()
# Define schema
schema = StructType([
StructField("id", LongType(), True),
StructField("arrays", ArrayType(DoubleType()), True)
])
# Sample data
data = [
(1, [18.2, 50.9]),
(2, [1.0, 3.4, 5.5]),
(3, [10.0, 20.1, 30.0])
]
# Create DataFrame
df = spark.createDataFrame(data, schema=schema)
I want a simple way to convert the arrays
item to be the average of the values in the array. For example, the first row would be 34.55.
I had tried something like this:
from pyspark.sql import functions as F
def average_arrays(df):
# Explode the array to individual elements
exploded_df = df.withColumn("arrays", F.explode("arrays"))
# Group by the original identifier and calculate the average then return as int
result_df = exploded_df.groupBy("id").agg(F.avg("arrays").alias("arrays"))
df = df.withColumn('arrays', col('arrays')[0].cast('int'))
return result_df
However, it still just returns the original array. I'd hugely appreciate any help here, thanks in advance.