3

Let's say I have a dataframe that looks like this:


df['GlobalName'][df['GlobalName']==''] = df['IsPerson'].apply(lambda x: x if x==True else '')

How can I apply lambda on the dataframe to make FullName = FirstName + ' ' + LastName? As far as I know lambda in dataframes has 1 input only? Thanks!

1 Answer 1

6

I think apply here is not neccesary, only join columns together with +:

df['FullName'] = df.FirstName + ' ' + df.LastName

Or use Series.str.cat:

df['FullName'] = df.FirstName.str.cat(df.LastName, sep=' ')

Solution with lambda is possible, but slow:

df['FullName'] = df.apply(lambda x: x.FirstName + ' ' + x.LastName, axis=1)
Sign up to request clarification or add additional context in comments.

9 Comments

Perfect! That worked :-) unfortunately I must use lambda as the problem I have is more complex (has if statement). I've posted that example for simplicity. A follow up question regarding speed...is lambda slow in general or in this specific case (when applied on dataframes)?
@ShadyMBA - It depends of formula. But generally is use apply it is slow, because loops under the hood.
@ShadyMBA - then use this
@ShadyMBA - Can you test df['GlobalName'] = np.where((df['GlobalName']=='') & (df['IsPerson']), df['CleanName'], '') ?
3 solutions: %timeit df['GlobalName'][df['GlobalName']==''] = df.apply(lambda x: x['CleanName'] if x['IsPerson'] == True else '', axis = 1) %timeit df['GlobalName'][df['GlobalName']==''] = np.where(df['IsPerson']==True, df['CleanName'], '') %timeit df['GlobalName'] = np.where((df['IsPerson']==True) & (df['GlobalName']==''), df['CleanName'], df['GlobalName']) 39.6 ms 40.6 ms 1.21 ms The 3rd solution is super fast compared to the other 2.
|

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.