<?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 how to convert each row of df to array of rows(list of rows) in Data Engineering</title>
    <link>https://community.databricks.com/t5/data-engineering/how-to-convert-each-row-of-df-to-array-of-rows-list-of-rows/m-p/26867#M18870</link>
    <description>&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;How to convert each row of dataframe to array of rows?&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Here is our scenario , we need to pass each row of dataframe to one function as dict to apply the key level transformations. But as our data is very huge we can't use collect  df.toJson().collect() to iterate over each row as it uses only driver's memory.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Please add your suggestions here.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Thank you&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;</description>
    <pubDate>Sun, 27 Feb 2022 14:15:09 GMT</pubDate>
    <dc:creator>SailajaB</dc:creator>
    <dc:date>2022-02-27T14:15:09Z</dc:date>
    <item>
      <title>how to convert each row of df to array of rows(list of rows)</title>
      <link>https://community.databricks.com/t5/data-engineering/how-to-convert-each-row-of-df-to-array-of-rows-list-of-rows/m-p/26867#M18870</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;How to convert each row of dataframe to array of rows?&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Here is our scenario , we need to pass each row of dataframe to one function as dict to apply the key level transformations. But as our data is very huge we can't use collect  df.toJson().collect() to iterate over each row as it uses only driver's memory.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Please add your suggestions here.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Thank you&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;</description>
      <pubDate>Sun, 27 Feb 2022 14:15:09 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/how-to-convert-each-row-of-df-to-array-of-rows-list-of-rows/m-p/26867#M18870</guid>
      <dc:creator>SailajaB</dc:creator>
      <dc:date>2022-02-27T14:15:09Z</dc:date>
    </item>
    <item>
      <title>Re: how to convert each row of df to array of rows(list of rows)</title>
      <link>https://community.databricks.com/t5/data-engineering/how-to-convert-each-row-of-df-to-array-of-rows-list-of-rows/m-p/26868#M18871</link>
      <description>&lt;P&gt;To optimize performance I would write pandas vectorized function and registered it as udf . &lt;A href="https://spark.apache.org/docs/latest/api/python/reference/api/pyspark.sql.functions.pandas_udf.html" target="test_blank"&gt;https://spark.apache.org/docs/latest/api/python/reference/api/pyspark.sql.functions.pandas_udf.html&lt;/A&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;If it is one time go you can also use basic spark function like foreach &lt;A href="https://spark.apache.org/docs/3.1.1/api/python/reference/api/pyspark.sql.DataFrame.foreach.html" target="test_blank"&gt;https://spark.apache.org/docs/3.1.1/api/python/reference/api/pyspark.sql.DataFrame.foreach.html&lt;/A&gt;&lt;/P&gt;</description>
      <pubDate>Sun, 27 Feb 2022 14:38:19 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/how-to-convert-each-row-of-df-to-array-of-rows-list-of-rows/m-p/26868#M18871</guid>
      <dc:creator>Hubert-Dudek</dc:creator>
      <dc:date>2022-02-27T14:38:19Z</dc:date>
    </item>
    <item>
      <title>Re: how to convert each row of df to array of rows(list of rows)</title>
      <link>https://community.databricks.com/t5/data-engineering/how-to-convert-each-row-of-df-to-array-of-rows-list-of-rows/m-p/26869#M18872</link>
      <description>&lt;P&gt;@Hubert Dudek​&amp;nbsp;, Thank you for the reply. &lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;We are new to ADB. And using the below code, looking for an optimized way to do it&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;dfJSONString = df.toJSON().collect()&lt;/P&gt;&lt;P&gt;stringList = []&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;for row in dfJSONString:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;# ==== Unflatten the JSON string ==== #&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;jsonString = unflatten(json.loads(row),dictreg[reg.upper()])&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;B&gt; &lt;/B&gt;&amp;nbsp;stringList.append(json.dumps(jsonString))&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Thank you&lt;/P&gt;</description>
      <pubDate>Mon, 28 Feb 2022 10:19:28 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/how-to-convert-each-row-of-df-to-array-of-rows-list-of-rows/m-p/26869#M18872</guid>
      <dc:creator>SailajaB</dc:creator>
      <dc:date>2022-02-28T10:19:28Z</dc:date>
    </item>
    <item>
      <title>Re: how to convert each row of df to array of rows(list of rows)</title>
      <link>https://community.databricks.com/t5/data-engineering/how-to-convert-each-row-of-df-to-array-of-rows-list-of-rows/m-p/26870#M18873</link>
      <description>&lt;P&gt;You have two options here:&lt;/P&gt;&lt;OL&gt;&lt;LI&gt;Regular PySpark UDF, where you define a function that takes one raw in your DF as input, and returns one row. &lt;A href="https://docs.microsoft.com/en-us/azure/databricks/spark/latest/spark-sql/udf-python" alt="https://docs.microsoft.com/en-us/azure/databricks/spark/latest/spark-sql/udf-python" target="_blank"&gt;See docs here&lt;/A&gt;.&lt;/LI&gt;&lt;LI&gt;Pandas UDF, where you define a function that takes a chunk of rows in your DF as input, and return rows. &lt;A href="https://docs.microsoft.com/en-us/azure/databricks/spark/latest/spark-sql/udf-python-pandas" alt="https://docs.microsoft.com/en-us/azure/databricks/spark/latest/spark-sql/udf-python-pandas" target="_blank"&gt;See docs here&lt;/A&gt;.&lt;/LI&gt;&lt;/OL&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Option 2 is most performant, but it involves operating on many rows. Still, a common approach is to define a row-level function in your PandasUDF and call it using an .apply. Take a look at the docs and try out some options. &lt;/P&gt;</description>
      <pubDate>Wed, 04 May 2022 16:10:27 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/how-to-convert-each-row-of-df-to-array-of-rows-list-of-rows/m-p/26870#M18873</guid>
      <dc:creator>Dan_Z</dc:creator>
      <dc:date>2022-05-04T16:10:27Z</dc:date>
    </item>
    <item>
      <title>Re: how to convert each row of df to array of rows(list of rows)</title>
      <link>https://community.databricks.com/t5/data-engineering/how-to-convert-each-row-of-df-to-array-of-rows-list-of-rows/m-p/26871#M18874</link>
      <description>&lt;P&gt;Hi @Sailaja B​&amp;nbsp;,&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Just a friendly follow-up. Do you still need help or the recommendations given By Dan or Hubert help you to resolve your issue? please let us know.&lt;/P&gt;</description>
      <pubDate>Tue, 10 May 2022 16:39:35 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/how-to-convert-each-row-of-df-to-array-of-rows-list-of-rows/m-p/26871#M18874</guid>
      <dc:creator>jose_gonzalez</dc:creator>
      <dc:date>2022-05-10T16:39:35Z</dc:date>
    </item>
    <item>
      <title>Re: how to convert each row of df to array of rows(list of rows)</title>
      <link>https://community.databricks.com/t5/data-engineering/how-to-convert-each-row-of-df-to-array-of-rows-list-of-rows/m-p/26872#M18875</link>
      <description>&lt;P&gt;Hi @Sailaja B​,&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Just a friendly follow-up. Did you saw Dan's response? Do you have any follow-up questions or can you select Dan's as best answer? &lt;/P&gt;</description>
      <pubDate>Tue, 07 Jun 2022 16:20:21 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/how-to-convert-each-row-of-df-to-array-of-rows-list-of-rows/m-p/26872#M18875</guid>
      <dc:creator>jose_gonzalez</dc:creator>
      <dc:date>2022-06-07T16:20:21Z</dc:date>
    </item>
  </channel>
</rss>

