title: How do i improve the speed of my code below to make changes in every cell of a column
body: <p>My current code:</p>
  <pre><code>for row in range(df1.shape[0]):
      words = df1.iloc[row,11].split()
      df1.iloc[row,11] = (&quot; &quot;.join(sorted(set(words), key=words.index)))
  </code></pre>
  <p>What it does its remove duplicate Country code within a string in a pandas data frame column so that they only appear once in the order of the sentence
  E.g.</p>
  <div class="s-table-container">
  <table class="s-table">
  <thead>
  <tr>
  <th>Countries</th>
  </tr>
  </thead>
  <tbody>
  <tr>
  <td>US CN US</td>
  </tr>
  <tr>
  <td>US CN EU</td>
  </tr>
  <tr>
  <td>US CN US EU</td>
  </tr>
  <tr>
  <td>US US US US</td>
  </tr>
  </tbody>
  </table>
  </div>
  <p>To be:</p>
  <div class="s-table-container">
  <table class="s-table">
  <thead>
  <tr>
  <th>Countries</th>
  </tr>
  </thead>
  <tbody>
  <tr>
  <td>US CN</td>
  </tr>
  <tr>
  <td>US CN EU</td>
  </tr>
  <tr>
  <td>US CN EU</td>
  </tr>
  <tr>
  <td>US</td>
  </tr>
  </tbody>
  </table>
  </div>
  <p>As can be seen iterating through 400k rows of data and editing them is extremely slow. Average of 20 mins per dataset.</p>
  <p>Hoping for any kind souls who could help me refine this further.</p>