Table:
+---------+---------+---------+---------+
|id |path |error |message |
+---------+---------+---------+---------+
| 1 | a.a.a| true | "aaa" |
+---------+---------+---------+---------+
| 2 | a.a.a| true | "bbb" |
+---------+---------+---------+---------+
| 2 | a.a.a| true | "bbc" |
+---------+---------+---------+---------+
| 2 | a.a.b| false | "ccc" |
+---------+---------+---------+---------+
I have pySpark query:
data.groupBy('id', 'path')\
.agg(
sum(when(col('error') == 'true', 1).otherwise(0)).alias('count'),
).show()
How can I add a new column with first example element that have col('error') == 'true'? I want a table with elements id, path, count, exampleItem.
Function first().alias('exampleItem') works, but returns elements that do not necessarily match the condition above.