130

I have a dataframe that may look like this:

A        B        C
foo      bar      foo bar
bar foo  foo      bar

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:

def foo_bar(x):
    return x.replace('foo', 'wow')

A        B        C
wow      bar      wow bar
bar wow  wow      bar

Is there a simple one-liner that can apply a function to each cell?

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.

eljusticiero67
  • 1,826
  • 3
  • 12
  • 16
  • 15
    I don't think it's a good idea to edit the questions to a completely new one, once you've already got answers to the old one as it would invalidate the prior answers to it. I would request you to roll back the original question and ask the new one separately. – Nickil Maveli Sep 13 '16 at 18:01

2 Answers2

193

You can use applymap() which is concise for your case.

df.applymap(foo_bar)

#     A       B       C
#0  wow     bar wow bar
#1  bar wow wow     bar

Another option is to vectorize your function and then use apply method:

import numpy as np
df.apply(np.vectorize(foo_bar))
#     A       B       C
#0  wow     bar wow bar
#1  bar wow wow     bar
normanius
  • 6,540
  • 4
  • 42
  • 72
Psidom
  • 195,464
  • 25
  • 298
  • 322
  • hi sorry, the question was about applying a function and not actually about figuring out which cell is even. let me see if i can change the example to be clearer.. – eljusticiero67 Sep 13 '16 at 17:48
  • 1
    If you are looking for applying a function, you can do `import numpy as np; df.apply(np.vectorize(iseven))` – Psidom Sep 13 '16 at 17:52
  • 2
    @eljusticiero67 You can use `df.applymap(iseven)` but note that it will become really slow for larger DataFrames so whenever you have the chance, use the vectorized methods. – ayhan Sep 13 '16 at 17:52
  • @ayhan -- your method seems to work, both with np.vectorize and without. Would you mind posting the answer so i can give you the credit? – eljusticiero67 Sep 13 '16 at 17:58
  • 1
    I actually didn't post it at first because I was trying to find a duplicate to link this to. I couldn't find an exact duplicate but maybe [this one](http://stackoverflow.com/questions/19798153/difference-between-map-applymap-and-apply-methods-in-pandas)? Nickil Maveli is right though, you can clarify the meaning but at least the example should be same I think. Maybe Psidom can edit to include their own suggestion along with applymap? – ayhan Sep 13 '16 at 18:14
  • awesome. apologies for the confusion with the examples. – eljusticiero67 Sep 13 '16 at 19:24
  • [Difference between map, applymap and apply methods in Pandas](https://stackoverflow.com/q/19798153/395857) – Franck Dernoncourt Jul 06 '17 at 00:20
  • I am new to Python and can't uderstand one thing. I used it df.applymap(foo_bar) but then when I print(df) it is not change. I must do like df = df.applymap(foo_bar) to make it work. Do we have to overwrite variables like this all the time or there is a better way for this in Pythoin? – heisenberg7584 Nov 03 '19 at 11:33
  • @heisenberg7584 This is because most Pandas method are not made 'inplace', but return a new object. To understand why, this discussion might help: [inplace is harmful](https://stackoverflow.com/questions/45570984/in-pandas-is-inplace-true-considered-harmful-or-not) – timmey Nov 06 '20 at 10:47
1

I guess you could use np.vectorize:

>>> df[:] = np.vectorize(foo_bar)(df)
>>> df
       A    B    C
foo  bar  wow  bar
bar  wow  wow  bar
>>> 

This might be quicker, since it's using numpy.

U12-Forward
  • 65,118
  • 12
  • 70
  • 89