Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using lambda if condition on different columns in Pandas dataframe

I have simple dataframe:

import pandas as pd
frame = pd.DataFrame(np.random.randn(4, 3), columns=list('abc'))

Thus for example:

a   b   c
0   -0.813530   -1.291862   1.330320
1   -1.066475   0.624504    1.690770
2   1.330330    -0.675750   -1.123389
3   0.400109    -1.224936   -1.704173

And then I want to create column “d” that contains value from “c” if c is positive. Else value from “b”.

I am trying:

frame['d']=frame.apply(lambda x: frame['c'] if frame['c']>0 else frame['b'],axis=0)

But getting “ValueError: ('The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().', 'occurred at index a')

I was trying to google how to solve this, but did not succeed. Any tip please?

like image 370
PeterL Avatar asked May 25 '16 16:05

PeterL


People also ask

How do I use lambda function on 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) . Yields below output.

How do you create a new column based on values from other columns in pandas?

Using apply() method If you need to apply a method over an existing column in order to compute some values that will eventually be added as a new column in the existing DataFrame, then pandas. DataFrame. apply() method should do the trick.


1 Answers

is that what you want?

In [300]: frame[['b','c']].apply(lambda x: x['c'] if x['c']>0 else x['b'], axis=1)
Out[300]:
0   -1.099891
1    0.582815
2    0.901591
3    0.900856
dtype: float64
like image 167
MaxU - stop WAR against UA Avatar answered Sep 30 '22 19:09

MaxU - stop WAR against UA