【发布时间】:2018-02-16 03:45:50
【问题描述】:
我正在尝试在 pandas df 上应用带有 if-else 条件的 lambda df 如下所示:
col1 col2 col3 col4 <---column names
None None None col4 <---column values in str
col1 None None None
None col2 None None
df_twitter_archive_master[['col1','col2','col3','col4']].apply(lambda x: x=0 if x=='None' else x=1)
基本上,它应该用 0 替换 'None' 值,否则用 1 但我不断收到此错误
df_twitter_archive_master[['col1','col2','col3','col4']].apply(lambda x: x=0 if x=='None' else x=1)
SyntaxError: invalid syntax
^ 在 x=1 之下
我做错了什么? ?
【问题讨论】:
-
你能发布一个带有预期输出的示例数据框吗?
-
好的,刚刚更新
-
虽然其余的答案都会为您提供结果,但您的问题来自语法
x=0和x=1。在您的 lambda 中,您不应该分配值。只需lambda x: 0 if x == 'None' else 1就足够了。