8

Consider the following data set stored in a pandas DataFrame dfX:

A   B
1   2
4   6
7   9

I have a function that is:

def someThingSpecial(x,y)
  # z = do something special with x,y
  return z

I now want to create a new column in df that bears the computed z value

Looking at other SO examples, I've tried several variants including:

dfX['C'] = dfX.apply(lambda x: someThingSpecial(x=x['A'], y=x['B']), axis=1)

Which returns errors. What is the right way to do this?

2
  • 1
    Might be useful to see what the errors are. Commented Dec 13, 2017 at 19:30
  • Good question. In trying to answer your point, I realized my lambda was correct - the error was in someThingSpecial and I got lost in the python error logs. Commented Dec 13, 2017 at 19:37

2 Answers 2

8

This seems to work for me on v0.21. Take a look -

df

   A  B
0  1  2
1  4  6
2  7  9

def someThingSpecial(x,y):
     return x + y


df.apply(lambda x: someThingSpecial(x.A, x.B), 1)

0     3
1    10
2    16
dtype: int64

You might want to try upgrading your pandas version to the latest stable release (0.21 as of now).


Here's another option. You can vectorise your function.

v = np.vectorize(someThingSpecial)

v now accepts arrays, but operates on each pair of elements individually. Note that this just hides the loop, as apply does, but is much cleaner. Now, you can compute C as so -

df['C'] = v(df.A, df.B)
Sign up to request clarification or add additional context in comments.

2 Comments

That works well, thank you! I like the elegance. As it turned out my original approach also worked - the error was in "somethingSpecial" but I changed to yours.
@user1361529 You're welcome. I've also found performance improvements with vectorize. Not sure what it does, but it's a real goto of mine.
3

if your function only needs one column's value, then do this instead of coldspeed's answer:

dfX['A'].apply(your_func)

to store it:

dfX['C'] = dfX['A'].apply(your_func)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.