I am new to Spark and I need some help regarding aggregation of values.
+--------------------+--------------------+-----+
| amount| transaction_code|Total|
+--------------------+--------------------+-----+
|[10, 20, 30, 40, ...|[buy, buy, sell, ...|210.0|
+--------------------+--------------------+-----+
I need to add a new column to this dataframe where I add the values present in amount if I see 'buy' in transaction_code For example, I add 10 and 20 as the transaction_code for them is 'buy'.
I know how to aggregate them totally and below is the code I have written.
df2extract = df2extract.select(
'amount',
'transaction_code',
F.expr('AGGREGATE(amount, cast(0 as float), (acc, x) -> acc + x)').alias('Total')
).show()
I found we can use if function but I am unable to determine how to initialize them and how to keep track of the amount . Please help me in this matter.Thanks a lot !