alias for count in Pyspark

Viewed 2922

I am new to Pyspark. I am trying to use alias for count function. For some reason if I use agg in front of count then alias is working but if I am not aggregating then the alias is giving me error.

.(count("firstName").alias("cnt"))

doesn't work;

.agg(count("firstName").alias("cnt"))

works.

I wanted to understand the issue with the 1st query.

1 Answers

You can try this:

.count().withColumnRenamed("count","cnt")

we cannot alias count function directly

Related