title: Find related value in a dataframe column
body: <p>I have a dataframe with IDs, names and flags.</p>
  <p>I want to create a new column, where related IDs would be filled in.</p>
  <p>Here are some rules:</p>
  <ul>
  <li>If the flag = '0' or 'A', then there is no related id</li>
  <li>If the flag = 'B' we need to find the same name (minus the flag in name) but with flag 'A' and get the id</li>
  <li>If the flag = 'C' we need to find the same name (minus the flag in name) but with flag 'B' and get the id</li>
  </ul>
  <p>Here is the code for the DF:</p>
  <pre><code>import pandas as pd
  
  d = {'name': ['test1 A', 'test1 B', 'test2 A', 'test2 B', 'test3','test4 A','test4 B','test4 C'],
       'id': [1, 2, 3, 4, 5, 6, 7, 8],
       'flag': ['A', 'B', 'A', 'B', '0', 'A', 'B', 'C']}
  df = pd.DataFrame(data=d)
  </code></pre>
  <p>And this is the desired result:</p>
  <pre><code>name        id     flag    related id
  test1 A      1        A           nan
  test1 B      2        B           1
  test2 A      3        A           nan
  test2 B      4        B           3
  test3 0      5        0           nan
  test4 A      6        A           nan
  test4 B      7        B           6
  test4 C      8        C           7
  </code></pre>
  <p>I was thining about removing the flag from the name and then somehow use groupby or merge, but have fallen short.</p>