title: Iterate over groups of rows in Pandas
body: <p>I am new to pandas and python in general - grateful for any direction you can provide! </p>
  
  <p>I have a csv file with 4 columns. I am trying to group together rows where the first three columns are the same on all rows (Column A Row 1 = Column A Row 2, Column B Row 1 = Column B Row 2, and so on)</p>
  
  <p>My data look like this:</p>
  
  <pre><code>   phone_number  state   date         description
  1  9991112222    NJ      2015-05-14   Condo
  2  9991112222    NJ      2015-05-14   Condo sales call
  3  9991112222    NJ      2015-05-14   Apartment rental
  4  6668885555    CA      2015-05-06   Apartment
  5  6668885555    CA      2015-05-06   Apartment rental
  6  4443337777    NJ      2015-05-14   condo
  </code></pre>
  
  <p>So in this data, rows 1, 2 and 3 would be in one group, and rows 4 and 5 would be in another group. Row 6 would not be in the group with 1, 2, and 3 because it has a different phone_number. </p>
  
  <p>Then, for each row, I want to compare the string in the description column against <em>each other description</em> in that group using Levenshtein distance, and keep the rows where the descriptions are sufficiently similar.  </p>
  
  <p>"Condo" from row 1 would be compared to "Condo sales call" from row 2 and to "Apartment rental" in row 3. It would not be compared to "condo" from row 6. </p>
  
  <p>In the end, the goal is to weed out rows where the description is not sufficiently similar to another description in the same group. Phrased differently, to print out all rows where description is at least somewhat similar to another (any other) description in that group. Ideal output:</p>
  
  <pre><code>   phone_number  state   date         description
  1  9991112222    NJ      2015-05-14   Condo
  2  9991112222    NJ      2015-05-14   Condo sales call
  4  6668885555    CA      2015-05-06   Apartment
  5  6668885555    CA      2015-05-06   Apartment rental
  </code></pre>
  
  <p>Row 6 does not print because it was never in a group. Row 3 doesn't print because "Apartment rental" is insufficiently similar to "Condo" or "Condo sales call"</p>
  
  <p>This is the code I have so far. I can't tell if this is the best way to do it. And if I have done it right so far, I can't figure out how to print the full row of interest:</p>
  
  <pre><code>import Levenshtein
  import itertools 
  import pandas as pd
  
  test_data = pd.DataFrame.from_csv('phone_state_etc_test.csv', index_col=None)
  
  for pn in test_data['phone_number']:
      for dt in test_data['date']:
          for st in test_data['state']:
              for a, b in itertools.combinations(test_data[
                                                       (test_data['phone_number'] == pn) &amp; 
                                                       (test_data['state'] == st) &amp; 
                                                       (test_data['date'] == dt)
                                                      ]
                                                       ['description'], 2):
                  if Levenshtein.ratio(a,b) &gt; 0.35:
                      print pn, "|", dt, "|", st, "|" #description
  </code></pre>
  
  <p>This prints a bunch of duplicates of these lines:</p>
  
  <pre><code>9991112222 | NJ | 2015-05-14 |
  6668885555 | CA | 2015-05-06 |
  </code></pre>
  
  <p>But if I add description to the end of the print line, I get a </p>
  
  <pre><code>SyntaxError: invalid syntax 
  </code></pre>
  
  <p>Any thoughts on how I can print the full row? Whether in pandas dataframe, or some other format, doesn't matter - I just need to output to csv.</p>