<?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 Re: Using &amp;quot;Select Expr&amp;quot; and &amp;quot;Stack&amp;quot; to Unpivot PySpark DataFrame doesn't produce in Data Engineering</title>
    <link>https://community.databricks.com/t5/data-engineering/using-quot-select-expr-quot-and-quot-stack-quot-to-unpivot/m-p/39717#M27059</link>
    <description>&lt;P&gt;on a another dummie example i can't reproduce this error, is there an explanation as to why this happens?&lt;/P&gt;</description>
    <pubDate>Sat, 12 Aug 2023 11:56:56 GMT</pubDate>
    <dc:creator>arggg</dc:creator>
    <dc:date>2023-08-12T11:56:56Z</dc:date>
    <item>
      <title>Using "Select Expr" and "Stack" to Unpivot PySpark DataFrame doesn't produce expected results</title>
      <link>https://community.databricks.com/t5/data-engineering/using-quot-select-expr-quot-and-quot-stack-quot-to-unpivot/m-p/17170#M11212</link>
      <description>&lt;P&gt;I am trying to unpivot a PySpark DataFrame, but I don't get the correct results.&lt;/P&gt;&lt;P&gt;Sample dataset:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;# Prepare Data
data = [("Spain", 101, 201, 301), \
        ("Taiwan", 102, 202, 302), \
        ("Italy", 103, 203, 303), \
        ("China", 104, 204, 304)
  ]
 
# Create DataFrame
columns= ["Country", "2018", "2019", "2002"]
df = spark.createDataFrame(data = data, schema = columns)
df.show(truncate=False)&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper" image-alt="image"&gt;&lt;img src="https://community.databricks.com/t5/image/serverpage/image-id/1014i91DB34D31880B7B4/image-size/large?v=v2&amp;amp;px=999" role="button" title="image" alt="image" /&gt;&lt;/span&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Below is the code I have tried:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;from pyspark.sql import functions as F
&amp;nbsp;
unpivotExpr = "stack(3, '2018', 2018, '2019', 2019, '2020', 2020) as (Year, CPI)"
unPivotDF = df.select("Country", F.expr(unpivotExpr))
unPivotDF.show()&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;And the results:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper" image-alt="image"&gt;&lt;img src="https://community.databricks.com/t5/image/serverpage/image-id/1015i97C140E04EF90A77/image-size/large?v=v2&amp;amp;px=999" role="button" title="image" alt="image" /&gt;&lt;/span&gt;As you can see in the above image, value of column "CPI" is the same as column "Year" which is not expected.&lt;/P&gt;&lt;P&gt;Any idea to solve this issue?&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;</description>
      <pubDate>Tue, 13 Dec 2022 05:07:10 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/using-quot-select-expr-quot-and-quot-stack-quot-to-unpivot/m-p/17170#M11212</guid>
      <dc:creator>Mado</dc:creator>
      <dc:date>2022-12-13T05:07:10Z</dc:date>
    </item>
    <item>
      <title>Re: Using "Select Expr" and "Stack" to Unpivot PySpark DataFrame doesn't produce expected results</title>
      <link>https://community.databricks.com/t5/data-engineering/using-quot-select-expr-quot-and-quot-stack-quot-to-unpivot/m-p/17171#M11213</link>
      <description>&lt;P&gt;Hi @Mohammad Saber​&amp;nbsp;&lt;/P&gt;&lt;P&gt;The issue is because the column name is similar to a literal value and it is taking that constant value for all the keys provided. &lt;/P&gt;&lt;P&gt;To avoid this you can give more proper column names like below. &lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper" image-alt="image"&gt;&lt;img src="https://community.databricks.com/t5/image/serverpage/image-id/1011i1684A99B274F1DE3/image-size/large?v=v2&amp;amp;px=999" role="button" title="image" alt="image" /&gt;&lt;/span&gt;Else if you have a way where you can explicitly say to spark that 2018 is the column value and not a literal, you can try that too..&lt;/P&gt;&lt;P&gt;Hope this helps...&lt;/P&gt;&lt;P&gt;Cheers&lt;/P&gt;</description>
      <pubDate>Tue, 13 Dec 2022 07:18:51 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/using-quot-select-expr-quot-and-quot-stack-quot-to-unpivot/m-p/17171#M11213</guid>
      <dc:creator>UmaMahesh1</dc:creator>
      <dc:date>2022-12-13T07:18:51Z</dc:date>
    </item>
    <item>
      <title>Re: Using "Select Expr" and "Stack" to Unpivot PySpark DataFrame doesn't produce</title>
      <link>https://community.databricks.com/t5/data-engineering/using-quot-select-expr-quot-and-quot-stack-quot-to-unpivot/m-p/39717#M27059</link>
      <description>&lt;P&gt;on a another dummie example i can't reproduce this error, is there an explanation as to why this happens?&lt;/P&gt;</description>
      <pubDate>Sat, 12 Aug 2023 11:56:56 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/using-quot-select-expr-quot-and-quot-stack-quot-to-unpivot/m-p/39717#M27059</guid>
      <dc:creator>arggg</dc:creator>
      <dc:date>2023-08-12T11:56:56Z</dc:date>
    </item>
    <item>
      <title>Re: Using "Select Expr" and "Stack" to Unpivot PySpark DataFrame doesn't produce</title>
      <link>https://community.databricks.com/t5/data-engineering/using-quot-select-expr-quot-and-quot-stack-quot-to-unpivot/m-p/54897#M30192</link>
      <description>&lt;P&gt;If i have columns names as below how can i get data&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;unpivottest &lt;/SPAN&gt;&lt;SPAN&gt;=&lt;/SPAN&gt; &lt;SPAN&gt;"stack(2,'Turnover (Sas&amp;nbsp; m)',Turnover (Sas&amp;nbsp; m),'abc %', abc %) as (kpi_name, kpi_value)"&lt;/SPAN&gt;&lt;SPAN&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;/SPAN&gt;&lt;/DIV&gt;&lt;/DIV&gt;</description>
      <pubDate>Fri, 08 Dec 2023 08:32:53 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/using-quot-select-expr-quot-and-quot-stack-quot-to-unpivot/m-p/54897#M30192</guid>
      <dc:creator>srinu1246</dc:creator>
      <dc:date>2023-12-08T08:32:53Z</dc:date>
    </item>
    <item>
      <title>Re: Using "Select Expr" and "Stack" to Unpivot PySpark DataFrame doesn't produce</title>
      <link>https://community.databricks.com/t5/data-engineering/using-quot-select-expr-quot-and-quot-stack-quot-to-unpivot/m-p/67987#M33506</link>
      <description>&lt;P&gt;You can also use backticks around the column names that would otherwise be recognised as numbers.&lt;/P&gt;&lt;PRE&gt;from pyspark.sql import functions as F
&amp;nbsp;
unpivotExpr = "stack(3, '2018', `2018`, '2019', `2019`, '2020', `2020`) as (Year, CPI)"
unPivotDF = df.select("Country", F.expr(unpivotExpr))
unPivotDF.show()&lt;/PRE&gt;</description>
      <pubDate>Fri, 03 May 2024 00:40:21 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/using-quot-select-expr-quot-and-quot-stack-quot-to-unpivot/m-p/67987#M33506</guid>
      <dc:creator>lukeoz</dc:creator>
      <dc:date>2024-05-03T00:40:21Z</dc:date>
    </item>
  </channel>
</rss>

