I ran this query in Spark
f"""
select
col_cate,
true as segment_true,
false as segment_false
from (
SELECT
{feature},
{target_col},
count(*) as cnt
from
table1
group by
{feature},
{target_col}
) pivot (
sum(cnt)
for target_bool in (true, false)
)
""")
The input data is this
+--------+-----------+
|col_cate|target_bool|
+--------+-----------+
| A| true|
| A| false|
| B| true|
| B| false|
| A| true|
| A| false|
| B| true|
| B| false|
+--------+-----------+
and the output data is
+--------+------------+-------------+
|col_cate|segment_true|segment_false|
+--------+------------+-------------+
| A| true| false|
| B| true| false|
+--------+------------+-------------+
However, I when I tried to do the same in pyspark, I can't figure out how to rename the outputs from [col_cate, true, false] to [col_cate, segment_true segment_false]
How do I do that?
I tried
output_df.groupBy(["col_cate", "target_bool"]).\
count().\
groupBy("col_cate").\
pivot("target_bool").\
sum("count")
but there doesn't seem to be a way to rename the column in the code. I know I can rename it after but it feels less elegant.