簡體   English   中英

Python // Lambda:語法無效

[英]Python // Lambda: invalid syntax

我正在嘗試在數據框上進行迭代。 我想用一個字符替換幾個字符,除非我要迭代的項目為null / nan / NaN / etc。

為此,我試圖在下面使用此行:

lista['ultima_receita'] = lista['ultima_receita'].apply(lambda rstr: float(rstr.replace('.','').replace(',','.')[3:]) if pd.isnull(rstr) == False)

但是,它一直讓我收到無效的Synthax錯誤:

    lista['ultima_receita'] = lista['ultima_receita'].apply(lambda rstr: float(rstr.replace('.','').replace(',','.')[3:]) if pd.isnull(rstr) == False)
                                                                                                                                                     ^
SyntaxError: invalid syntax

我已經盡了一切努力,也沒有找到導致合成膠錯誤的原因。 有人可以幫忙嗎?

嘗試簡化您的問題。 lambda確實是問題所在,因此與此處的熊貓無關。

>>> lambda rstr: float(rstr.replace('.','').replace(',','.')[3:]) if pd.isnull(rstr) == False
SyntaxError: invalid syntax  # At the end of 'False' above

甚至更簡單:

>>> lambda x: "foo" if "bar" == False
SyntaxError: invalid syntax

這是因為Python需要一個elseA if B else C建設。 如果您想進行條件修改,則可以將其else rstr ,或使用其他pandas / numpy邏輯來執行不同的邏輯。

>>> func = lambda x: "foo" if "bar" in x else x
>>> func("isobaric"), func("agnostic")
('foo', 'agnostic')

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM