Python Lambda 包括 if...elif...else
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/44991438/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Lambda including if...elif...else
提问by 2Obe
I want to apply a lambda function to a DataFrame column using if...elif...else within the lambda function.
我想在 lambda 函数中使用 if...elif...else 将 lambda 函数应用于 DataFrame 列。
The df and the code are smth. like:
df 和代码很简单。喜欢:
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 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....elif....else 应用到 lambda?如何使用 List Comprehension 获得相同的结果?
Thanks for any response.
感谢您的任何回应。
回答by Uriel
Nest if .. else
s:
巢if .. else
:
lambda x: x*10 if x<2 else (x**2 if x<4 else x+10)
回答by cs95
I do not recommend the use of apply
here: it should be avoided if there are better alternatives.
我不推荐使用apply
这里:如果有更好的选择应该避免使用。
For example, if you are performing the following operation on a Series:
例如,如果您正在对一个系列执行以下操作:
if cond1:
exp1
elif cond2:
exp2
else:
exp3
This is usually a good use case for np.where
or np.select
.
这通常是np.where
or 的一个很好的用例np.select
。
numpy.where
numpy.where
The if
else
chain above can be written using
if
else
上面的链可以写成
np.where(cond1, exp1, np.where(cond2, exp2, ...))
np.where
allows nesting. With one level of nesting, your problem can be solved with,
np.where
允许嵌套。通过一层嵌套,您的问题可以解决,
df['three'] = (
np.where(
df['one'] < 2,
df['one'] * 10,
np.where(df['one'] < 4, df['one'] ** 2, df['one'] + 10))
df
one two three
0 1 6 10
1 2 7 4
2 3 8 9
3 4 9 14
4 5 10 15
numpy.select
numpy.select
Allows for flexible syntax and is easily extensible. It follows the form,
允许灵活的语法并且易于扩展。它遵循形式,
np.select([cond1, cond2, ...], [exp1, exp2, ...])
Or, in this case,
或者,在这种情况下,
np.select([cond1, cond2], [exp1, exp2], default=exp3)
df['three'] = (
np.select(
condlist=[df['one'] < 2, df['one'] < 4],
choicelist=[df['one'] * 10, df['one'] ** 2],
default=df['one'] + 10))
df
one two three
0 1 6 10
1 2 7 4
2 3 8 9
3 4 9 14
4 5 10 15
and
/or
(similar to the if
/else
)
and
/ or
(类似于if
/ else
)
Similar to if-else
, requires the lambda
:
类似于if-else
,需要lambda
:
df['three'] = df["one"].apply(
lambda x: (x < 2 and x * 10) or (x < 4 and x ** 2) or x + 10)
df
one two three
0 1 6 10
1 2 7 4
2 3 8 9
3 4 9 14
4 5 10 15
List Comprehension
列表理解
Loopy solution that is stillfaster than apply
.
糊涂的解决方案,仍然快于apply
。
df['three'] = [x*10 if x<2 else (x**2 if x<4 else x+10) for x in df['one']]
# df['three'] = [
# (x < 2 and x * 10) or (x < 4 and x ** 2) or x + 10) for x in df['one']
# ]
df
one two three
0 1 6 10
1 2 7 4
2 3 8 9
3 4 9 14
4 5 10 15
回答by plfrick
For readability I prefer to write a function, especially if you are dealing with many conditions. For the original question:
为了可读性,我更喜欢写一个函数,特别是当你处理许多条件时。对于原始问题:
def parse_values(x):
if x < 2:
return x * 10
elif x < 4:
return x ** 2
else:
return x + 10
df['one'].apply(parse_values)