Use conditional summation as you would do in SQL :
from pyspark.sql import functions as F
df1 = df.select(
F.sum(F.when(F.col("matches") == 0, 1).otherwise(0)).alias("0 matches"),
F.sum(F.when(F.col("matches").between(1, 2), 1).otherwise(0)).alias("lessthan3"),
F.sum(F.when(F.col("matches") >= 3, 1).otherwise(0)).alias("morethan3")
).drop("names")
df1.show()
#+---------+---------+---------+
#|0 matches|lessthan3|morethan3|
#+---------+---------+---------+
#| 0| 1| 3|
#+---------+---------+---------+
Another way of doing this is to group by the ranges and count:
df1 = df.withColumn(
"range",
F.when(F.col("matches") == 0, "0 matches")
.when(F.col("matches").between(1, 2), "lessthan3")
.when(F.col("matches") >= 3, "morethan3")
).groupBy("range").count()
#+---------+-----+
#| range|count|
#+---------+-----+
#|lessthan3| 1|
#|morethan3| 3|
#+---------+-----+
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…