title: Flagging Pandas row based on same and other row
body: <p>I would like to add a new column to flag rows in a dataframe. The flag is supposed to be done on the following logic:</p>
  
  <ol>
  <li>Rows with the same ID belong together and should be flagged in the same way</li>
  <li>The flag is constructed from four cells with different combinations of "receive" then "float" or "fixed" followed by ", pay" and then "float" or "fixed"</li>
  </ol>
  
  <p>I think an example might make this more clear. This would be the original data frame.</p>
  
  <pre><code>df = pd.DataFrame(data=[[2, 'fix','receive'], [2, 'float','pay'], [3, 'fix','receive'], [3, 'fix','pay'], [7, 'float','pay'], [7, 'float','receive']], columns=["ID", "Structure","Leg"])
  </code></pre>
  
  <p>And this should be the result after applying the above logic and creating a new column that flags each row:</p>
  
  <pre><code>df["Flag"] = ["Receive fix, pay float", "Receive fix, pay float", "Receive fix, pay fix","Receive fix, pay fix","Receive float, pay float","Receive float, pay float"]
  </code></pre>
  
  <p>So, my main questions is how I can loop through the dataframe to find two rows with the same ID and then use information from those two rows to create the same flag for each of the two rows. Thanks a lot for your ideas.</p>
  
  <p>I don't know if this goes into the right direction, but this is my attempt. The problem remains as to how to get the data from the second row that has the same ID.</p>
  
  <pre><code>df["Flag"] = "???"
  for index, row in df.iterrows():
      if row["Leg"] == "receive":
          df.at[index, "Flag"] = row["Leg"] + " " + row["Structure"] + ", pay ?"
  </code></pre>