カウント
グループ内のアイテムの数を返します。
構文
Python
from pyspark.sql import functions as sf
sf.count(col)
パラメーター
パラメーター | Type | 説明 |
|---|---|---|
|
| ターゲットカラムをコンピュートに。 |
戻り値
pyspark.sql.Column: コンピュート結果の列。
例
例1 : DataFrame内のすべての行を数える
Python
from pyspark.sql import functions as sf
df = spark.createDataFrame([(None,), ("a",), ("b",), ("c",)], schema=["alphabets"])
df.select(sf.count(sf.expr("*"))).show()
Output
+--------+
|count(1)|
+--------+
| 4|
+--------+
例2 : 特定の列の非NULL値を数える
Python
from pyspark.sql import functions as sf
df.select(sf.count(df.alphabets)).show()
Output
+----------------+
|count(alphabets)|
+----------------+
| 3|
+----------------+
例3 : 複数の列を持つDataFrame内のすべての行を数える
Python
from pyspark.sql import functions as sf
df = spark.createDataFrame(
[(1, "apple"), (2, "banana"), (3, None)], schema=["id", "fruit"])
df.select(sf.count(sf.expr("*"))).show()
Output
+--------+
|count(1)|
+--------+
| 3|
+--------+
例4 : 複数の列の非NULL値を数える
Python
from pyspark.sql import functions as sf
df.select(sf.count(df.id), sf.count(df.fruit)).show()
Output
+---------+------------+
|count(id)|count(fruit)|
+---------+------------+
| 3| 2|
+---------+------------+