title: regarding the ntile function in pyspark windows function
body: <p>I am running the following code segment,</p>
  <pre><code>import pyspark
  from pyspark.sql import SparkSession
  from pyspark.sql.window import Window
  from pyspark.sql.functions import ntile
  spark = SparkSession.builder.appName('SparkByExamples.com').getOrCreate()
  
  simpleData = ((&quot;James&quot;, &quot;Sales&quot;, 3000), \
      (&quot;Michael&quot;, &quot;Sales&quot;, 4600),  \
      (&quot;Robert&quot;, &quot;Sales&quot;, 4100),   \
      (&quot;Maria&quot;, &quot;Finance&quot;, 3000),  \
      (&quot;James&quot;, &quot;Sales&quot;, 3000),    \
      (&quot;Scott&quot;, &quot;Finance&quot;, 3300),  \
      (&quot;Jen&quot;, &quot;Finance&quot;, 3900),    \
      (&quot;Jeff&quot;, &quot;Marketing&quot;, 3000), \
      (&quot;Kumar&quot;, &quot;Marketing&quot;, 2000),\
      (&quot;Saif&quot;, &quot;Sales&quot;, 4100) \
    )
   
  columns= [&quot;employee_name&quot;, &quot;department&quot;, &quot;salary&quot;]
  df = spark.createDataFrame(data = simpleData, schema = columns)windowSpec  = 
  Window.partitionBy(&quot;department&quot;).orderBy(&quot;salary&quot;)
  df.withColumn(&quot;ntile&quot;,ntile(2).over(windowSpec)).show()
  </code></pre>
  <p>The result is shown as follows, and I am confused on how does ntile(2) works, why the first and second rows get nitle column value as <code>1</code>, and why the third row and fourth row get <code>1</code> and <code>2</code> respectively even though they have the same salary value. I just cannot figure out how does this ntile columns calculated?</p>
  <p><a href="https://i.stack.imgur.com/7rx6L.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/7rx6L.png" alt="enter image description here" /></a></p>