<?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 Loop through Dataframe in Python in Data Engineering</title>
    <link>https://community.databricks.com/t5/data-engineering/loop-through-dataframe-in-python/m-p/27130#M19015</link>
    <description>&lt;P&gt;&lt;/P&gt;
&lt;P&gt;Hello,&lt;/P&gt;
&lt;P&gt;Imagine you have a dataframe with cols: A, B, C. I want to add a column D based on some calculations of columns B and C of the previous record of the df. Which is the best way of doing this? I am trying to avoid looping through the df. I am using python.&lt;/P&gt;
&lt;P&gt;Thanks.&lt;/P&gt;
&lt;P&gt;Fernando.&lt;/P&gt; 
&lt;P&gt;&lt;/P&gt;</description>
    <pubDate>Wed, 10 Jun 2020 01:08:04 GMT</pubDate>
    <dc:creator>FernandoBenedet</dc:creator>
    <dc:date>2020-06-10T01:08:04Z</dc:date>
    <item>
      <title>Loop through Dataframe in Python</title>
      <link>https://community.databricks.com/t5/data-engineering/loop-through-dataframe-in-python/m-p/27130#M19015</link>
      <description>&lt;P&gt;&lt;/P&gt;
&lt;P&gt;Hello,&lt;/P&gt;
&lt;P&gt;Imagine you have a dataframe with cols: A, B, C. I want to add a column D based on some calculations of columns B and C of the previous record of the df. Which is the best way of doing this? I am trying to avoid looping through the df. I am using python.&lt;/P&gt;
&lt;P&gt;Thanks.&lt;/P&gt;
&lt;P&gt;Fernando.&lt;/P&gt; 
&lt;P&gt;&lt;/P&gt;</description>
      <pubDate>Wed, 10 Jun 2020 01:08:04 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/loop-through-dataframe-in-python/m-p/27130#M19015</guid>
      <dc:creator>FernandoBenedet</dc:creator>
      <dc:date>2020-06-10T01:08:04Z</dc:date>
    </item>
    <item>
      <title>Re: Loop through Dataframe in Python</title>
      <link>https://community.databricks.com/t5/data-engineering/loop-through-dataframe-in-python/m-p/27131#M19016</link>
      <description>&lt;P&gt;&lt;/P&gt;
&lt;P&gt;I would probably use a window function within pyspark.&lt;/P&gt;
&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;Link to Databricks blog: &lt;A href="https://databricks.com/blog/2015/07/15/introducing-window-functions-in-spark-sql.html" target="test_blank"&gt;https://databricks.com/blog/2015/07/15/introducing-window-functions-in-spark-sql.html&lt;/A&gt;
&lt;P&gt;Another option is to use lag or lead columns to help you capture the data in the relative position you desire.&lt;/P&gt;
&lt;P&gt;You can find theme here in the SQL functions list: &lt;A href="https://docs.databricks.com/spark/latest/spark-sql/language-manual/functions.html" target="test_blank"&gt;https://docs.databricks.com/spark/latest/spark-sql/language-manual/functions.html&lt;/A&gt;&lt;/P&gt; 
&lt;P&gt;&lt;/P&gt;</description>
      <pubDate>Wed, 10 Jun 2020 01:52:29 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/loop-through-dataframe-in-python/m-p/27131#M19016</guid>
      <dc:creator>ColbyCarrillo</dc:creator>
      <dc:date>2020-06-10T01:52:29Z</dc:date>
    </item>
    <item>
      <title>Re: Loop through Dataframe in Python</title>
      <link>https://community.databricks.com/t5/data-engineering/loop-through-dataframe-in-python/m-p/27132#M19017</link>
      <description>&lt;P&gt;&lt;/P&gt;
&lt;P&gt; Iterating through pandas dataFrame objects is generally slow. &lt;A target="_blank" href="https://"&gt;Pandas Iteration&lt;/A&gt; beats the whole purpose of using DataFrame. It is an anti-pattern and is something you should only do when you have exhausted every other option. It is better look for a List Comprehensions , vectorized solution or DataFrame.apply() method. &lt;/P&gt;
&lt;P&gt; Pandas DataFrame loop using list comprehension: &lt;/P&gt;
&lt;P&gt; result = [(x, y,z) for x, y,z in zip(df['Name'], df['Promoted'],df['Grade'])] &lt;/P&gt; 
&lt;P&gt;&lt;/P&gt;
&lt;P&gt;&lt;/P&gt;</description>
      <pubDate>Mon, 03 May 2021 06:25:39 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/loop-through-dataframe-in-python/m-p/27132#M19017</guid>
      <dc:creator>quincybatten</dc:creator>
      <dc:date>2021-05-03T06:25:39Z</dc:date>
    </item>
  </channel>
</rss>

