Apply function to each cell in DataFrame
<p>I have a dataframe that may look like this:</p>

<pre><code>A        B        C
foo      bar      foo bar
bar foo  foo      bar
</code></pre>

<p>I want to look through every element of each row (or every element of each column) and apply the following function to get the subsequent DF:</p>

<pre><code>def foo_bar(x):
    return x.replace('foo', 'wow')

A        B        C
wow      bar      wow bar
bar wow  wow      bar
</code></pre>

<p>Is there a simple one-liner that can apply a function to each cell? </p>

<p>This is a simplistic example so there may be an easier way to execute this specific example other than applying a function, but what I am really asking about is how to apply a function in every cell within a dataframe. </p>

<p>You can do <code>df%2</code>, with the old question about finding the even numbers in the data frame:</p>

<pre><code>df%2 == 0

#    A        B       C
#0  True    False   True
#1  False   True    False
</code></pre>

<p><em>Update</em>:</p>

<p>Since the question has been updated, as per @ayhan suggested, you can use <code>applymap()</code> which is concise for your case. </p>

<pre><code>df.applymap(foo_bar)

#     A       B       C
#0  wow     bar wow bar
#1  bar wow wow     bar
</code></pre>

<p>Another option is to vectorize your function and then use <code>apply</code> method:</p>

<pre><code>import numpy as np
df.apply(np.vectorize(foo_bar))
#     A       B       C
#0  wow     bar wow bar
#1  bar wow wow     bar
</code></pre>

python pandas dataframe apply