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
¶ To create a new column, use the [] brackets with the new column name at the left side of the assignment.
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) .
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With