I have pyspark dataframe:
+---+------------------+
|Id |Friend_Ids|Points |
+------+-------+-------+
|1 |[2, 5] | 5 |
|2 |[1, 3, 4] | 6 |
|3 |[2, 4] | 2 |
|4 |[3, 5, 2] | 12 |
|5 |[1, 4] | 1 |
+------+-------+-------+
I want to get a column with sum of points of friends of each id:
+---+------------------+-------------------+
|Id |Friend_Ids|Points |Friends_points_sum |
+------+-------+-------+-------------------+
|1 |[2, 5] | 5 | 7 |
|2 |[1, 3, 4] | 6 | 19 |
|3 |[2, 4] | 2 | 18 |
|4 |[2, 3, 5] | 12 | 9 |
|5 |[1, 4] | 1 | 17 |
+------+-------+-------+-------------------+
I have tried this
df.withColumn("friends_points_sum",df.filter(F.col('Id').isin(F.col('Friends_Ids'))\
.agg(F.sum('points')).collect()[0][0])
and get TypeError: 'Column' object is not callable
I have also tried udf like
def sum_agg(array, df):
id = array[0]
friend_ids = array[1]
points = array[2]
s = df.filter(F.col(id).isin(friend_id)).agg(F.sum('points')).collect()[0][0]
return s.tolist()
points_sum = F.udf(qnty_agg, IntegerType())
df.withColumn("friends_points_sum", qnty_sum(F.array('id','Friend_Ids','Points'), df))
but it does not accept df as an argument