【发布时间】:2018-03-16 22:51:16
【问题描述】:
这是我的数据集
fake_abalone2
Sex Length Diameter Height Whole Shucked Viscera Shell Rings
Weight Weight Weight Weight
0 M 0.455 0.365 0.095 0.5140 0.2245 0.1010 0.1500 15
1 M 0.350 0.265 0.090 0.2255 0.0995 0.0485 0.0700 7
2 F 0.530 0.420 0.135 0.6770 0.2565 0.1415 0.2100 9
3 M 0.440 0.365 0.125 0.5160 0.2155 0.1140 0.1550 10
4 K 0.330 0.255 0.080 0.2050 0.0895 0.0395 0.0550 7
5 K 0.425 0.300 0.095 0.3515 0.1410 0.0775 0.1200 8
使用以下方法时出现语法错误。请帮帮我。 我希望“sex”表中的值根据“Rings”表进行更改。如果“Rings”值小于 10,则相应的“sex”值应更改为“K”。否则,不应在“性”表。
fake_abalone2["sex"]=fake_abalone2["Rings"].apply(lambda x:"K" if x<10)
文件“”,第 1 行 fake_abalone2["sex"]=fake_abalone2["Rings"].apply(lambda x:"K" if x
SyntaxError: 无效语法
【问题讨论】:
-
欢迎来到 StackOverflow!请格式化您的问题,不要忘记阅读How to ask 指南。
-
如果
x >= 10,lambda 的输出应该是什么? -
对于 x>=10,“性别”列中不应有任何变化
标签: python-3.x if-statement lambda pandas-apply