Catatan
Akses ke halaman ini memerlukan otorisasi. Anda dapat mencoba masuk atau mengubah direktori.
Akses ke halaman ini memerlukan otorisasi. Anda dapat mencoba mengubah direktori.
Mengembalikan jumlah nilai TRUE untuk kolom .
Syntax
from pyspark.sql import functions as sf
sf.count_if(col)
Parameter-parameternya
| Pengaturan | Tipe | Description |
|---|---|---|
col |
pyspark.sql.Column atau nama kolom |
Kolom target untuk dikerjakan. |
Pengembalian Barang
pyspark.sql.Column: jumlah TRUE nilai untuk col.
Examples
Contoh 1: Menghitung jumlah angka genap dalam kolom numerik
from pyspark.sql import functions as sf
df = spark.createDataFrame([("a", 1), ("a", 2), ("a", 3), ("b", 8), ("b", 2)], ["c1", "c2"])
df.select(sf.count_if(sf.col('c2') % 2 == 0)).show()
+------------------------+
|count_if(((c2 % 2) = 0))|
+------------------------+
| 3|
+------------------------+
Contoh 2: Menghitung jumlah baris di mana kolom string dimulai dengan huruf tertentu
from pyspark.sql import functions as sf
df = spark.createDataFrame(
[("apple",), ("banana",), ("cherry",), ("apple",), ("banana",)], ["fruit"])
df.select(sf.count_if(sf.col('fruit').startswith('a'))).show()
+------------------------------+
|count_if(startswith(fruit, a))|
+------------------------------+
| 2|
+------------------------------+
Contoh 3: Menghitung jumlah baris di mana kolom numerik lebih besar dari nilai tertentu
from pyspark.sql import functions as sf
df = spark.createDataFrame([(1,), (2,), (3,), (4,), (5,)], ["num"])
df.select(sf.count_if(sf.col('num') > 3)).show()
+-------------------+
|count_if((num > 3))|
+-------------------+
| 2|
+-------------------+
Contoh 4: Menghitung jumlah baris di mana kolom boolean adalah True
from pyspark.sql import functions as sf
df = spark.createDataFrame([(True,), (False,), (True,), (False,), (True,)], ["b"])
df.select(sf.count('b'), sf.count_if('b')).show()
+--------+-----------+
|count(b)|count_if(b)|
+--------+-----------+
| 5| 3|
+--------+-----------+