Search code examples
countpysparkalias

alias for count in Pyspark


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.


Solution

  • You can try this:

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

    we cannot alias count function directly