Pandas: strip numbers and parenthesis from string
<p>My pandas df:</p>

<pre><code>df  = pd.DataFrame({'A':[1,2,3,4,5], 'B':['(AAAAA)2','(BCA)1','(CA)5','(DD)8','(ED)15']})
    A   B
0   1   (AAAAA)2
1   2   (BCA)1
2   3   (CA)5
3   4   (DD)8
4   5   (ED)15
</code></pre>

<p>I want to strip parenthsis and numbers in the column <code>B</code> <br>
Expected output is:</p>

<pre><code>    A   B
0   1   AAAAA
1   2   BCA
2   3   CA
3   4   DD
4   5   ED
</code></pre>

<p>So far I tried, </p>

<pre><code>df['B'] = df['B'].str.extract('([ABCDE])')
</code></pre>

<p>But I only got:</p>

<pre><code>    A   B
0   1   A
1   2   B
2   3   C
3   4   D
4   5   E
</code></pre>

<p>you can do it this way:</p>

<pre><code>In [388]: df
Out[388]:
   A         B
0  1  (AAAAA)2
1  2    (BCA)1
2  3     (CA)5
3  4     (DD)8
4  5    (ED)15

In [389]: df.B = df.B.str.replace(r'[\(\)\d]+', '')

In [390]: df
Out[390]:
   A      B
0  1  AAAAA
1  2    BCA
2  3     CA
3  4     DD
4  5     ED
</code></pre>

<p>if you still want to use <code>.str.extract()</code> you can do it this way:</p>

<pre><code>In [401]: df['B'].str.extract(r'.*?([A-Za-z]+).*?', expand=True)
Out[401]:
       0
0  AAAAA
1    BCA
2     CA
3     DD
4     ED
</code></pre>

python regex pandas dataframe python-3.5