1

consider a dataframe:

x = pd.DataFrame({'a' :[1,2,3], 'b':['za','zb','zc']})

when I tried

x['b'].apply(str.upper)

it is working fine.

but when i tried

x['b'].apply(str.replace('z',''))

it is throwing an error.

Can any one help me what are the functions we can use inside apply and map functions in pandas

Thanks in advance.

Saravanan
  • 132
  • 2
  • 10

2 Answers2

1

In pandas is better use str accessor functions:

print(x['b'].str.upper())
0    ZA
1    ZB
2    ZC
Name: b, dtype: object

print(x['b'].str.replace('z',''))
0    a
1    b
2    c
Name: b, dtype: object

But if need apply add lambda, but it failed if some NaNs values:

print(x['b'].apply(lambda x: x.replace('z','')))
0    a
1    b
2    c
Name: b, dtype: object
jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252
  • Thats fine bro. but I want to know why we can't use that inside apply. Else tell me what kind of functions we can use inside apply. – Saravanan Nov 29 '17 at 06:55
  • 1
    The reason is because you need to call `str.replace` with its arguments at the time you apply it, not at the time you pass it to apply. You can use a `lambda` as @jezrael suggested or you can use `functools.partial` – Imran Nov 29 '17 at 07:00
  • 1
    @Imran - Thank you. – jezrael Nov 29 '17 at 07:02
  • 1
    @Saravanan - In my opinion generally in `apply` with one column (Series) is possible use functions working elementwise, but in pandas is always better first use vectorized functions and then loops (apply under the hood are loops), Jeff, one of dev of pandas now, it describe [here](https://stackoverflow.com/a/24871316/2901002) – jezrael Nov 29 '17 at 07:06
  • Thank you @jezrael – Saravanan Nov 29 '17 at 08:13
0

To clarify what is actually causing your error: you can use any function within apply, but str.replace('z', '') is not a valid statement. This will throw an error on its own.

replace is a bound method for string objects. It is meant to be called on a string object, not on the string type:

'xyz'.replace('z', '')    # Returns 'xy'
str.replace('z', '')      # Throws TypeError (not enough arguments)

Neither of the above statements return a function, so the result cannot be used with apply. As in the answer by @jezrael, to use replace with apply, you need a function which takes one argument, such as a lambda.

The reason str.upper works, despite also being a bound method, is that it does not take any other arguments, and therefore can be called with one argument. So str.upper('xyz') is equivalent to the proper usage 'xyz'.upper(). You would see the same error as with your use of replace if you tried x['b'].apply(str.upper())

Simon Bowly
  • 1,003
  • 5
  • 10