<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Using Window with a dynamic size do not work in Get Started Discussions</title>
    <link>https://community.databricks.com/t5/get-started-discussions/using-window-with-a-dynamic-size-do-not-work/m-p/51045#M6128</link>
    <description>&lt;P&gt;Hello&lt;/P&gt;&lt;P&gt;I having an issue with using a window in pyspark. I created a simple example that reproduce the error, basically i want to define a window with a dynamic size (say from another column ) instead of a fix value&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;from pyspark.sql import SparkSession
from pyspark.sql.functions import col, when
from pyspark.sql.window import Window
from pyspark.sql import functions as F

# Create a Spark session
spark = SparkSession.builder.appName("example").getOrCreate()

data = [(3, 1), (3, 1), (3, 1), (3, 1), (2, 0), (2, 1), (2, 1), (2, 0)]
df = spark.createDataFrame(data, ["col1", "col2"])
print(df.show())

# Add a column 'col3' based on the second column with a dynamic window size
win = Window().orderBy("col1").rowsBetween(0, F.col("col1"))

df_with_custom_column = df.withColumn(
    "col3",
    F.when(
        F.count("col2").over(win)
        == F.col("col1"),
        True
    ).otherwise(False)
)


df_with_custom_column.show()&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I keep getting this error :&lt;STRONG&gt;&amp;nbsp;&lt;SPAN class=""&gt;ValueError&lt;/SPAN&gt;: Cannot convert column into bool: please use '&amp;amp;' for 'and', '|' for 'or', '~' for 'not' when building DataFrame boolean expressions.&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;Thank you so much for your replies&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;Amine&lt;/SPAN&gt;&lt;/P&gt;</description>
    <pubDate>Mon, 13 Nov 2023 13:17:55 GMT</pubDate>
    <dc:creator>AmineHY</dc:creator>
    <dc:date>2023-11-13T13:17:55Z</dc:date>
    <item>
      <title>Using Window with a dynamic size do not work</title>
      <link>https://community.databricks.com/t5/get-started-discussions/using-window-with-a-dynamic-size-do-not-work/m-p/51045#M6128</link>
      <description>&lt;P&gt;Hello&lt;/P&gt;&lt;P&gt;I having an issue with using a window in pyspark. I created a simple example that reproduce the error, basically i want to define a window with a dynamic size (say from another column ) instead of a fix value&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;from pyspark.sql import SparkSession
from pyspark.sql.functions import col, when
from pyspark.sql.window import Window
from pyspark.sql import functions as F

# Create a Spark session
spark = SparkSession.builder.appName("example").getOrCreate()

data = [(3, 1), (3, 1), (3, 1), (3, 1), (2, 0), (2, 1), (2, 1), (2, 0)]
df = spark.createDataFrame(data, ["col1", "col2"])
print(df.show())

# Add a column 'col3' based on the second column with a dynamic window size
win = Window().orderBy("col1").rowsBetween(0, F.col("col1"))

df_with_custom_column = df.withColumn(
    "col3",
    F.when(
        F.count("col2").over(win)
        == F.col("col1"),
        True
    ).otherwise(False)
)


df_with_custom_column.show()&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I keep getting this error :&lt;STRONG&gt;&amp;nbsp;&lt;SPAN class=""&gt;ValueError&lt;/SPAN&gt;: Cannot convert column into bool: please use '&amp;amp;' for 'and', '|' for 'or', '~' for 'not' when building DataFrame boolean expressions.&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;Thank you so much for your replies&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;Amine&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Mon, 13 Nov 2023 13:17:55 GMT</pubDate>
      <guid>https://community.databricks.com/t5/get-started-discussions/using-window-with-a-dynamic-size-do-not-work/m-p/51045#M6128</guid>
      <dc:creator>AmineHY</dc:creator>
      <dc:date>2023-11-13T13:17:55Z</dc:date>
    </item>
  </channel>
</rss>

