title: How can I Replace an address to have only numbers and some letters using str.replace() function in Python?
body: <p>I am trying to match the left and tight addresses (from separate tables) on a reference index (coClean) which I created with the following formula in #Python #JupyterNotebook</p>
  <pre><code>import pandas as pd
  df1=pd.read_csv(&quot;/content/Addmatchdf1.csv&quot;)
  df2=pd.read_csv(&quot;/content/Addmatchdf2.csv&quot;)
  
  import re
  def cleanAddress(series):
  return series.str.lower().str.replace(r&quot;[a-z\s\,]&quot;,&quot;&quot;)
  df1[&quot;coClean&quot;]=cleanAddress(df1[&quot;Address&quot;])
  
  df1[&quot;coClean&quot;]=cleanAddress(df1[&quot;Address&quot;]) 
  df = pd.merge(df1, df2, 
                        on =['coClean'], 
                        how ='inner') 
  </code></pre>
  <p>This produces a coClean as a reference index.</p>
  <div class="s-table-container">
  <table class="s-table">
  <thead>
  <tr>
  <th>Address_x</th>
  <th>coClean</th>
  <th>Address_y</th>
  </tr>
  </thead>
  <tbody>
  <tr>
  <td>7 Pindara Bvd LANGWARRIN  VIC  3910</td>
  <td>73910</td>
  <td>7 Pindara Blv, Langwarrin, VIC 3910</td>
  </tr>
  <tr>
  <td>2a Manor St BACCHUS MARSH VIC 3340</td>
  <td>23340</td>
  <td>2a Manor Street, BACCHUS MARSH, VIC 3340</td>
  </tr>
  <tr>
  <td>38 Sommersby Rd POINT COOK  VIC  3030</td>
  <td>383030</td>
  <td>38 Sommersby Road, Point Cook, VIC 3030</td>
  </tr>
  <tr>
  <td>17 Moira Avenue, Carnegie, Vic 3163</td>
  <td>173163</td>
  <td>17 Moira Avenue, Carnegie, Vic 3163</td>
  </tr>
  <tr>
  <td>17 Moira Avenue, Carnegie, Vic 3163</td>
  <td>173163</td>
  <td>17 Newman Avenue, Carnegie, VIC 3163</td>
  </tr>
  <tr>
  <td>17 Moira Avenue, Carnegie, Vic 3163</td>
  <td>173163</td>
  <td>17 Maroona Rd, Carnegie VIC 3163</td>
  </tr>
  </tbody>
  </table>
  </div>
  <p>The problem I am facing is, apparently, some addresses under the same postcode have the same house number. But than the joining becomes difficult as the reference index is same.</p>
  <p>How can i revise this function so that the reference index contains only</p>
  <pre><code>a. the house numbers
  b. first four letters
  c. postcode
  </code></pre>
  <p>So a new reference for '23340'(2a manor street bacchus marsh vic 3340) becomes
  '2aman3340'? So a list is returned like:</p>
  <div class="s-table-container">
  <table class="s-table">
  <thead>
  <tr>
  <th>coClean</th>
  </tr>
  </thead>
  <tbody>
  <tr>
  <td>7pind3910</td>
  </tr>
  <tr>
  <td>2aman3340</td>
  </tr>
  <tr>
  <td>38somm3030</td>
  </tr>
  <tr>
  <td>17moir3163</td>
  </tr>
  <tr>
  <td>17newm3163</td>
  </tr>
  <tr>
  <td>17maroo3163</td>
  </tr>
  </tbody>
  </table>
  </div>
  <p>I attempted to revise the function to include all letters and numbers</p>
  <pre><code>def cleanAddress(series):
  return series.str.lower().str.replace(r&quot;[^a-z\d]&quot;,&quot;&quot;)
  </code></pre>
  <p>But including all letters will not solve the problem as different tables contain street as st. and road as rd. Hence, a better strategy is to rely on house numbers and postcodes with some initial letters.</p>
  <p>Thank you for your kind suggestions.</p>
  <p>Update:
  I replaced</p>
  <pre><code>def cleanAddress(series):
  return series.str.lower().str.replace(r&quot;[a-z\s\,]&quot;,&quot;&quot;)
  df1[&quot;coClean&quot;]=cleanAddress(df1[&quot;Address&quot;])
  </code></pre>
  <p>with</p>
  <pre><code>def cleanAddress(series):
      coclen=&quot;&quot;
      number_of_letters=0
      if series:
          for i in range(len(series)):
              if series[i].isnumeric():
                  coclen+=series[i]
              elif series[i].isalpha():
                  number_of_letters+=1
                  coclen+=series[i]
                  if number_of_letters==4:
                      break
          for i in range(i,len(series)):
              if series[i].isnumeric():
                  coclen+=series[i]
      return coclen
  </code></pre>
  <p>This returns an error while I execute</p>
  <pre><code>cleanAddress(df1[&quot;Address&quot;])
  
  The full error is as follows:
  ---------------------------------------------------------------------------
  ValueError                                Traceback (most recent call last)
  &lt;ipython-input-17-b653a19f5638&gt; in &lt;module&gt;()
  ----&gt; 1 df1[&quot;coClean&quot;]=cleanAddress(df1[&quot;Address&quot;])
  
  1 frames
  /usr/local/lib/python3.7/dist-packages/pandas/core/generic.py in __nonzero__(self)
     1328     def __nonzero__(self):
     1329         raise ValueError(
  -&gt; 1330             f&quot;The truth value of a {type(self).__name__} is ambiguous. &quot;
     1331             &quot;Use a.empty, a.bool(), a.item(), a.any() or a.all().&quot;
     1332         )
  
  ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().
  </code></pre>