I am trying to implement if-elif or if-else logic in python while working on a dataframe. I am struggling when working with more than one column.
sample data frame
df=pd.DataFrame({"one":[1,2,3,4,5],"two":[6,7,8,9,10], "name": 'a', 'b', 'a', 'b', 'c'})
If my if-else logic is based on only one column - I know how to do it.
df['one'] = df["one"].apply(lambda x: x*10 if x<2 else (x**2 if x<4 else x+10))
But I want to modify column 'one' based on values of column 'two' - and I feel its going be something like this -
lambda x, y: x*100 if y>8 else (x*1 if y<8 else x**2)
But I am not sure how to specify the second column. I tried this way but obviously that's incorrect
df['one'] = df["one"]["two"].apply(lambda x, y: x*100 if y>8 else (x*1 if y<8 else x**2))
Question 1 - what'd be the correct syntax for the above code ?
Question 2 - How to implement below logic using lambda ?
if df['name'].isin(['a','b']) df['one'] = 100 else df['one'] = df['two']
If I write something like x.isin(['a','b']) it won't work.