タイル
ウィンドウ関数: 順序付けられたウィンドウ パーティション内の ntile グループ ID (1 からnまで) を返します。たとえば、 nが 4 の場合、行の最初の 4 分の 1 には値 1 が、2 番目の 4 分の 1 には値 2 が、3 番目の 4 分の 1 には値 3 が、最後の 4 分の 1 には値 4 が設定されます。
これは SQL の NTILE 関数と同等です。
構文
Python
from pyspark.sql import functions as sf
sf.ntile(n)
パラメーター
パラメーター | Type | 説明 |
|---|---|---|
| int | パーティションを分割するグループの数を指定する整数。 |
戻り値
pyspark.sql.Column: 分割されたグループ ID。
例
Python
from pyspark.sql import functions as sf
from pyspark.sql import Window
df = spark.createDataFrame(
[("a", 1), ("a", 2), ("a", 3), ("b", 8), ("b", 2)], ["c1", "c2"])
df.show()
Output
+---+---+
| c1| c2|
+---+---+
| a| 1|
| a| 2|
| a| 3|
| b| 8|
| b| 2|
+---+---+
Python
w = Window.partitionBy("c1").orderBy("c2")
df.withColumn("ntile", sf.ntile(2).over(w)).show()
Output
+---+---+-----+
| c1| c2|ntile|
+---+---+-----+
| a| 1| 1|
| a| 2| 1|
| a| 3| 2|
| b| 2| 1|
| b| 8| 2|
+---+---+-----+