title: Python: Grouping similar words with sentences in pandas
body: <p>I have a database with sentences and often only words. Often I have words like purchase and purchases. When I count the words, I have both purchase and purchases, which distorts the calculation. my need is as follows:</p>
  <p>I want to loop on my columns, and the first time I notice a word, I replace the similar word in the other sentences. I tried with fuzzy, but I only get words at the end and no sentence</p>
  <p>For example :</p>
  <p><code>This topic is about purchasing</code></p>
  <p><code>He was talking about shopping</code></p>
  <p>It becomes:</p>
  <p><code>This topic is about purchasing</code></p>
  <p><code>He was talking about purchasing</code></p>
  <p>Even if the sentence is distorted, that's okay.</p>
  <p><a href="https://i.stack.imgur.com/afRLd.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/afRLd.png" alt="Data sample" /></a></p>
  <p>I applied this code, but the result is not satisfactory:</p>
  <pre><code>import pandas
  from fuzzywuzzy import fuzz
  
  # Replaces %90 and more similar strings  
  def func(input_list):
      for count, item in enumerate(input_list):
          rest_of_input_list = input_list[:count] + input_list[count + 1:]
          new_list = []
          for other_item in rest_of_input_list:
              similarity = fuzz.ratio(item, other_item)
              if similarity &gt;= 90:
                  new_list.append(item)
              else:
                  new_list.append(other_item)
          input_list = new_list[:count] + [item] + new_list[count :]
                  
      return input_list
  
  df = pandas.read_csv('input.txt') # Read data from csv
  result = []
  for column in list(df):
      column_values = list(df[column])
      first_words = [x[:x.index(&quot; &quot;)] if &quot; &quot; in x else x for x in column_values]
      result.append(func(first_words))
      
  new_df = pandas.DataFrame(result).transpose() 
  new_df.columns = list(df)
  
  print(new_df)
  </code></pre>