Lambda包括if ... elif ... else [英] Lambda including if...elif...else
本文介绍了Lambda包括if ... elif ... else的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想使用lambda函数中的if ... elif ... else将lambda函数应用于DataFrame列.
I want to apply a lambda function to a DataFrame column using if...elif...else within the lambda function.
df和代码不完整.像:
The df and the code are smth. 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)
显然,这种方式不起作用. 如果.... elif .... else不能用于lambda,有什么方法可以应用? 如何使用List Comprehension关联相同的结果?
obviously this way it is not working. Is there a way to apply if....elif....else to lambda? How can I relize the same result with List Comprehension?
感谢您的任何回应.
推荐答案
嵌套if .. else
s:
lambda x: x*10 if x<2 else (x**2 if x<4 else x+10)
这篇关于Lambda包括if ... elif ... else的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文