Creating a new column in Panda using lambda function on two existing columns

I can add a new column to Panda by specifying a user function and then applying apply. However, I want to do this using lambda; is there any way?

For example, df has two columns a and b . I want to create a new column c that is equal to the longest length between a and b .

Some things like:

 df['c'] = df.apply(lambda x, len(df['a']) if len(df['a']) > len(df['b']) or len(df['b']) ) 

One approach:

 df = pd.DataFrame({'a':['dfg','f','fff','fgrf','fghj'], 'b' : ['sd','dfg','edr','df','fghjky']}) df['c'] = df.apply(lambda x: max([len(x) for x in [df['a'], df['b']]])) print df abc 0 dfg sd NaN 1 f dfg NaN 2 fff edr NaN 3 fgrf df NaN 4 fghj fghjky NaN 
+6
source share
1 answer

You can use the map function and select the np.where function np.where more detail

 print df # ab #0 aaa rrrr #1 bb k #2 ccc e #condition if condition is True then len column a else column b df['c'] = np.where(df['a'].map(len) > df['b'].map(len), df['a'].map(len), df['b'].map(len)) print df # abc #0 aaa rrrr 4 #1 bb k 2 #2 ccc e 3 

The following solution with the apply function with parameter axis=1 :

axis = 1 or 'columns: apply a function to each row

 df['c'] = df.apply(lambda x: max(len(x['a']), len(x['b'])), axis=1) 
+6
source

All Articles