Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/354.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 我们可以在lambda函数上使用多分支条件吗?_Python_Pandas - Fatal编程技术网

Python 我们可以在lambda函数上使用多分支条件吗?

Python 我们可以在lambda函数上使用多分支条件吗?,python,pandas,Python,Pandas,我正在使用以下代码计算一个新的Pandas列 temp['UFrio'] = temp['TempC'].map(lambda x: 0 if (x <0) math.sin((2*math.pi/28)*x) elif (x<25) else -1) temp['UFrio']=temp['TempC'].map(λx:0如果(x这样写: def thingy(x): if x < 0: return 0 elif x < 25:

我正在使用以下代码计算一个新的Pandas列

temp['UFrio'] = temp['TempC'].map(lambda x: 0 if (x <0) math.sin((2*math.pi/28)*x) elif (x<25) else -1)
temp['UFrio']=temp['TempC'].map(λx:0如果(x这样写:

def thingy(x):
    if x < 0:
        return 0
    elif x < 25:
        return math.sin((2*math.pi/28)*x)
    else:
        return -1
temp['UFrio'] = temp['TempC'].map(thingy)
def thingy(x):
如果x<0:
返回0
elif x<25:
返回math.sin((2*math.pi/28)*x)
其他:
返回-1
temp['UFrio']=temp['TempC'].map(thingy)
很可能你能想出一个比thingy更好的名字。没有必要把所有这些都写进lambda中。

这样写:

def thingy(x):
    if x < 0:
        return 0
    elif x < 25:
        return math.sin((2*math.pi/28)*x)
    else:
        return -1
temp['UFrio'] = temp['TempC'].map(thingy)
temp['UFrio'] = temp['TempC'].map(lambda x: 0 if (x <0)  else x)
temp['UFrio'] = temp['TempC'].map(lambda x: math.sin((2*math.pi/28)*x) if (x<25)  else -1)
def thingy(x):
如果x<0:
返回0
elif x<25:
返回math.sin((2*math.pi/28)*x)
其他:
返回-1
temp['UFrio']=temp['TempC'].map(thingy)

很可能你能想出一个比thingy更好的名字。没有必要把所有这些都放到lambda中。

temp['UFrio']=temp['TempC'].map(lambda x:0 if(x
temp['UFrio']=temp['TempC'].map)(lambda x:0 if(x
0 if x<0 else math.sin)(2*math.pi/28)*x)如果x<25 else-1
@behzad.nouri:请发布一个答案,以便投票。@behzad.nouri谢谢。它起作用了。@Bakuriu no!
1如果为真,否则2如果为假,否则3
给出
1
如果x<0 else math.sin((2*math.pi/28)*x)如果x<25 else-1
@behzad.nouri:请发布一个答案,这样它就可以被投票支持。@behzad.nouri谢谢你。它起作用了。@Bakuriu no!
1如果为真,否则2如果为假,否则3
给出了
1
,而这个代码片段可以解决这个问题,确实有助于提高你的帖子的质量。记住,你是在为我回答这个问题将来的读者,而这些人可能不知道您的代码建议的原因。虽然此代码片段可能会解决问题,但确实有助于提高您的文章质量。请记住,您是在将来为读者回答问题,而这些人可能不知道您的代码建议的原因。
temp['UFrio'] = temp['TempC'].map(lambda x: 0 if (x <0)  else x)
temp['UFrio'] = temp['TempC'].map(lambda x: math.sin((2*math.pi/28)*x) if (x<25)  else -1)