Issue
I want to apply a lambda function to a DataFrame column using if...elif...else within the lambda function.
The df and the code are something like:
df=pd.DataFrame({"one":[1,2,3,4,5],"two":[6,7,8,9,10]})
df["one"].apply(lambda x: x*10 if x<2 elif x<4 x**2 else x+10)
Obviously, this doesn't work. Is there a way to apply if....elif....else to a lambda? How can I get the same result with List Comprehension?
Solution
Nest if .. else
s:
lambda x: x*10 if x<2 else (x**2 if x<4 else x+10)
Answered By - Uriel
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.