尝试使用 Pandas Lambda 更改行值时出现语法错误

Syntax error when attempting to change row values with Pandas Lambda

我 运行 遇到以下语法错误:

df['storetype'] = df.apply(lambda x: "TP" if x.storetype == "MT", axis=1)

我正在尝试检查 storetype 列中的值是否为“MT”,如果是,则将其更改为“TP”。 不太熟悉 lambda 函数,因此不胜感激。

如评论中所述,此三元形式需要 else。如果你想在 else 情况下什么都不做,你可以用 else x.storetype:

返回原始值
df['storetype'] = df.apply(lambda x: "TP" if x.storetype == "MT" else x.storetype, axis=1)