title: how to join text column based on the difference of others columns?
body: <p>I have the following dataframe</p>
  <pre><code>       top   bottom                      fontname    size       x0       x1                           text
  0   62.890   73.890  HNGMRP+HelveticaNeueLTStd-Bd  11.000  321.730  520.115  RISK MANAGEMENT AND INTERNAL 
  1   76.893   87.893  HNGMRP+HelveticaNeueLTStd-Bd  11.000  321.730  376.334                        CONTROL
  2  146.897  157.897  HNGMRP+HelveticaNeueLTStd-Bd  11.000   76.535  203.662              COMPANY SECRETARY
  3  272.913  283.913  HNGMRP+HelveticaNeueLTStd-Bd  11.000   76.535  222.593          INDEPENDENT AUDITORS 
  4  286.916  297.916  HNGMRP+HelveticaNeueLTStd-Bd  11.000   76.535  167.164                   REMUNERATION
  </code></pre>
  <p>I want to</p>
  <ol>
  <li>join the text of <code>row[i].text</code> and <code>row[i+1].text</code> if <code>abs(row[i].bottom - row[i+1].top) &lt; row[i].size</code></li>
  <li>replace <code>joined_text</code> to <code>row[i].text</code></li>
  <li>replace <code>row[i].bottom</code> to <code>row[i+1].bottom</code></li>
  <li>discard <code>row[i+1]</code></li>
  </ol>
  <p>For example:</p>
  <ul>
  <li><code>row 0</code> has <code>text</code>: <code>RISK MANAGEMENT AND INTERNAL </code></li>
  <li><code>row 1</code> has <code>text</code>: <code>CONTROL</code></li>
  <li><code>row 0</code> and <code>row 1</code> both have same <code>size</code>: 11</li>
  <li><code>abs(row[0].bottom - row[1].top)</code> is 3.003</li>
  </ul>
  <p>Because <code>3.003 &lt; 11</code></p>
  <ul>
  <li>desired <code>row[0].text</code> is <code>RISK MANAGEMENT AND INTERNAL CONTROL</code></li>
  <li>desired <code>row[0].bottom</code> is <code>87.893</code></li>
  <li><code>row[1]</code> is removed from the dataframe</li>
  </ul>
  <p>For clarity, the desired result is the following:</p>
  <pre><code>       top   bottom                      fontname    size       x0       x1                           text
  0   62.890   87.893  HNGMRP+HelveticaNeueLTStd-Bd  11.000  321.730  520.115  RISK MANAGEMENT AND INTERNAL CONTROL
  1  146.897  157.897  HNGMRP+HelveticaNeueLTStd-Bd  11.000   76.535  203.662  COMPANY SECRETARY
  2  272.913  297.916  HNGMRP+HelveticaNeueLTStd-Bd  11.000   76.535  222.593  INDEPENDENT AUDITORS REMUNERATION
  </code></pre>
  <p>It is what I attempted:</p>
  <pre><code>def df_section_text(self) -&gt; pd.DataFrame:
      df_title_text = self.df_title_text
      df_next_title_text = self.df_title_text.shift(-1).dropna()
      df_section_text = pd.DataFrame()
      
      for next_title, title in zip(df_next_title_text.itertuples(index=False),  df_title_text.itertuples(index=False)):
          diff_btw_titles = abs(title.bottom - next_title.top)
          
          if diff_btw_titles &lt; title.size:
              title = pd.DataFrame([title]).to_dict()
              title['bottom'][0] = next_title.bottom
              title['text'][0] += next_title.text
              title = pd.DataFrame.from_dict(title)
          
          df_section_text = df_section_text.append([title])
      
      df_section_text = df_section_text.drop_duplicates(subset=['bottom']).reset_index()
      return df_section_text
  </code></pre>
  <p>where <code>self.df_title_text</code> is the problem dataframe presented above.</p>
  <p>It is  slow when the row number increases. Is there another faster and more elegant way that produces the desired result? Thank you.</p>