Python loop through Dataframe 'Series' object has no attribute
<p>Using pandas version 0.19.0, I have a dataframe with compiled regular expressions inside.  I want to loop over the dataframe and see if any of the regular expressions match a value.  I can do it with two for loops, but I can't figure out how to do it so that it'll return a same sized dataframe.</p>

<pre><code>import pandas as pd
import re

inp = [{'c1':re.compile('a'), 'c2':re.compile('b')}, {'c1':re.compile('c'),'c2':re.compile('d')}, {'c1':re.compile('e'),'c2':re.compile('f')}]
df = pd.DataFrame(inp)
for i,v in df.items():
  for a in v:
    if (a.match('a')):
      print("matched")
    else:
      print("failed")
</code></pre>

<p>This fails:</p>

<pre><code>[a.match('a') for a in [v for i,v in df.items()]]
</code></pre>

<blockquote>
  <p>AttributeError: 'Series' object has no attribute 'match'</p>
</blockquote>

<p>What I want:</p>

<pre><code>[a.match('a') for a in [v for i,v in df.items()]]
              c1                                         c2
0   &lt;_sre.SRE_Match object; span=(0, 1), match='a'&gt;     None
1   None                                                None
2   None                                                None
</code></pre>

<p>It looks like you need to use the <code>applymap</code> method. See the docs <a href="http://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.applymap.html?highlight=applymap" rel="nofollow">here</a> for more info. </p>

<pre><code>df.applymap(lambda x: x.match('a'))
</code></pre>

<p>Output:</p>

<p><a href="https://i.stack.imgur.com/J5twk.png" rel="nofollow"><img src="https://i.stack.imgur.com/J5twk.png" alt="enter image description here"></a></p>

python pandas dataframe