Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I am able to add a new column in Panda by defining user function and then using apply. However, I want to do this using lambda; is there a way around?

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

Some thing 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       a       b   c 0   dfg      sd NaN 1     f     dfg NaN 2   fff     edr NaN 3  fgrf      df NaN 4  fghj  fghjky NaN 
like image 514
piyush sharma Avatar asked Nov 12 '15 20:11

piyush sharma


People also ask

How do you make new columns derived from existing columns in Python?

¶ To create a new column, use the [] brackets with the new column name at the left side of the assignment.

How do I apply a lambda function to a column in pandas?

Apply Lambda Function to Single Column You can apply the lambda function for a single column in the DataFrame. The following example subtracts every cell value by 2 for column A – df["A"]=df["A"]. apply(lambda x:x-2) .


1 Answers

You can use function map and select by function np.where more info

print df #     a     b #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 #     a     b  c #0  aaa  rrrr  4 #1   bb     k  2 #2  ccc     e  3 

Next solution is with function apply with parameter axis=1:

axis = 1 or ‘columns’: apply function to each row

df['c'] = df.apply(lambda x: max(len(x['a']), len(x['b'])), axis=1) 
like image 159
jezrael Avatar answered Sep 22 '22 22:09

jezrael