title: Python - Pandas - String Replacement with regex | (OR)
body: <p>I've got 2 Dataframes :</p>
  <pre><code>df_bisID = pd.DataFrame({'A': ['ID1#ID2', 'ID3#ID4'],
                           'B': ['ID5#ID6', 'ID7#ID8'],
                           'C': ['ID9#ID10', 'ID11#ID12'],
                           'D': ['ID13#ID14', 'ID15#ID16']})
  
      A       B       C           D
  0   ID1#ID2 ID5#ID6 ID9#ID10    ID13#ID14
  1   ID3#ID4 ID7#ID8 ID11#ID12   ID15#ID16
  
  
  df_eliminateID = pd.DataFrame({'A': ['ID1', 'ID3', 'ID9', 'ID11'],
                                 'B': ['ID5', 'ID7', 'ID13', 'ID15']})
  
      A    B
  0   ID1  ID5
  1   ID3  ID7
  2   ID9  ID13
  3   ID11 ID15
  
  </code></pre>
  <p>The IDs are complicated, with alphanumerics, and unique  ('xxxx-xxxxxxxxxx-xxx'+ facultative suffix '-xxx-xxx'). I propose an example with simplified IDs.</p>
  <p>In df_bisID I need to get rid of one of both ID and #, in each column, where it's respectively contained in columns A or B in df_eliminateID.</p>
  <p>So I want to replace 'ID1#ID2' by 'ID2' and 'ID9#ID10' by 'ID10', etc.</p>
  <p>Note that it also could be 'ID2#ID1' instead of 'ID1#ID2' and it's complicating the replacement conditions, that's also the cause of this question string replacement.</p>
  <p>A logical solution to my problem is :</p>
  <pre><code>for i in df_eliminateID['A']:
      df_bisID.replace(i+'#|#'+i, '')
  for i in df_eliminateID['B']:
      df_bisID.replace(i+'#|#'+i, '')
  </code></pre>
  <p>But at the end, my df_bisID is empty, that's why I ask your help with the use of | regex in my string replacement, I want to replace IDX# or #IDX by '' to conserve only one of the IDs.
  The OR is | in regex, maybe I didn't used it the right way (is there an exclusif or in regex ?)</p>
  <p>In advance, thanks for your help.</p>
  <p><strong>EDIT: A SOLUTION FOUND !</strong></p>
  <p>I had to replace some '_' into '-' previously in my dataframe with</p>
  <pre><code>df.replace('_', '-', regex=True)
  </code></pre>
  <p>If regex=False, it doesn't work, I do not understand exactly why (if somebody could explain it to me it would be really cool, I searched in the docs but with no gains in terms of comprehension)</p>
  <p>So I decided to test if it would work on this problem to :</p>
  <pre><code>dfbis = pd.DataFrame({'A': ['ID1#ID2', 'ID3#ID4'],
                        'B': ['ID5#ID6', 'ID7#ID8'],
                        'C': ['ID9#ID20', 'ID21#ID22'],
                        'D': ['ID23#ID24', 'ID25#ID26']})
      A       B       C           D
  0   ID1#ID2 ID5#ID6 ID9#ID20  ID23#ID24
  1   ID3#ID4 ID7#ID8 ID21#ID22 ID25#ID26
  
  dfdetermine = pd.DataFrame({'A': ['ID1', 'ID3', 'ID20', 'ID22'],
                              'B': ['ID5', 'ID7', 'ID23', 'ID25']})
  
      A    B
  0   ID1  ID5
  1   ID3  ID7
  2   ID20 ID23
  3   ID22 ID25
  
  for i in dfdetermine['A'], dfdetermine['B']:
      print(i)
      dfbis = dfbis.replace(i+'#', '', regex=True).replace('#'+i, '', regex=True)
  
  dfbis
  
      A   B   C    D
  0   ID2 ID6 ID9  ID24
  1   ID4 ID8 ID21 ID26
  </code></pre>
  <p>I changed the ID10+ into ID20+, if not the ID1 test was perturbing the result.</p>
  <p>Also, I found something about string replacement timing that inspired me to put appart the IDX# and #IDX replacement into 2 calls to df.replace : <a href="https://qastack.fr/programming/3411771/best-way-to-replace-multiple-characters-in-a-string" rel="nofollow noreferrer">https://qastack.fr/programming/3411771/best-way-to-replace-multiple-characters-in-a-string</a></p>
  <p>Do you think my program could be optimised ?</p>