Let's say I have a dataframe that looks like this:
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!
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)
apply it is slow, because loops under the hood.df['GlobalName'] = np.where((df['GlobalName']=='') & (df['IsPerson']), df['CleanName'], '') ?%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.