TypeError: expected string or bytes-like object pandas variable
<p>I have dataset like this</p>

<pre><code>import pandas as pd
df = pd.DataFrame({'word': ['abs e learning ', 'abs e-learning', 'abs e&amp;learning', 'abs elearning']})
</code></pre>

<p>I want to get  </p>

<pre><code>      word
0   abs elearning
1   abs elearning
2   abs elearning
3   abs elearning
</code></pre>

<p>I do as bellow</p>

<pre><code>re_map = {r'\be learning\b': 'elearning', r'\be-learning\b': 'elearning', r'\be&amp;learning\b': 'elearning'}
import re
for r, map in re_map.items():
            df['word'] = re.sub(r, map, df['word'])
</code></pre>

<p>and error</p>

<pre><code>TypeError                                 Traceback (most recent call last)
&lt;ipython-input-42-fbf00d9a0cba&gt; in &lt;module&gt;()
      3 s = df['word']
      4 for r, map in re_map.items():
----&gt; 5             df['word'] = re.sub(r, map, df['word'])

C:\Users\Edward\Anaconda3\lib\re.py in sub(pattern, repl, string, count, flags)
    180     a callable, it's passed the match object and must return
    181     a replacement string to be used."""
--&gt; 182     return _compile(pattern, flags).sub(repl, string, count)
    183 
    184 def subn(pattern, repl, string, count=0, flags=0):

TypeError: expected string or bytes-like object
</code></pre>

<p>I can apply str like this</p>

<pre><code>for r, map in re_map.items():
            df['word'] = re.sub(r, map, str(df['word']))
</code></pre>

<p>There is no mistake but i cann't get pd.dataFrame as i wish</p>

<pre><code>    word
0   0 0 0 abs elearning \n1 abs elearning\...\n1 0 0 abs elearning \n1 abs elearning\...\n2 0 0 abs elearning \n1 abs ele...
1   0 0 0 abs elearning \n1 abs elearning\...\n1 0 0 abs elearning \n1 abs elearning\...\n2 0 0 abs elearning \n1 abs ele...
2   0 0 0 abs elearning \n1 abs elearning\...\n1 0 0 abs elearning \n1 abs elearning\...\n2 0 0 abs elearning \n1 abs ele...
3   0 0 0 abs elearning \n1 abs elearning\...\n1 0 0 abs elearning \n1 abs elearning\...\n2 0 0 abs elearning \n1 abs ele...
</code></pre>

<p>how to improve it?</p>

<p><code>df['word']</code> is a list. Converting to string just destroys your list.</p>

<p>You need to apply regex on each member:</p>

<pre><code>for r, map in re_map.items():
    df['word'] = [re.sub(r, map, e) for e in df['word']]:
</code></pre>

<p>classical alternate method without list comprehension:</p>

<pre><code> for r, map in re_map.items():
     d = df['word']
     for i,e in enumerate(d):
         d[i] = re.sub(r, map, e)
</code></pre>

<p>BTW you could simplify your regex list drastically:</p>

<pre><code>re_map = {r'\be[\-&amp; ]learning\b': 'elearning'}
</code></pre>

<p>By doing that you only have one regex and this becomes a one-liner:</p>

<pre><code> df['word'] = [re.sub(r'\be[\-&amp; ]learning\b', 'elearning', e) for e in df['word']]:
</code></pre>

<p>could even be faster by pre-compiling the regex once for all substitutions:</p>

<pre><code> theregex = re.compile(r'\be[\-&amp; ]learning\b')
 df['word'] = [theregex.sub('elearning', e) for e in df['word']]:
</code></pre>

python regex