Chadee Fouad
Chadee Fouad

Reputation: 2938

Pandas dataframe apply lambda based on inputs from multiple columns

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!

Upvotes: 3

Views: 285

Answers (1)

jezrael
jezrael

Reputation: 862481

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)

Upvotes: 6

Related Questions