python - 在 If-Elif-Else 语句中未正确运行的赋值语句
问题描述
我正在尝试根据“合同”和“任期”列值中的值创建一个新列(称为“合同结束”)。
如果 Contract 的值为“两年”并且tenure%24 为 0、1 或 23(即 23、24、25、47、48、49、59、60 或 61),我想设置该值对于 ContractEnding 为 1。
如果我在 if 语句中添加 print 语句,它将打印,但值永远不会改变。它们是 if 语句中唯一的语句,因此它们都应该运行。
谁能帮我弄清楚我做错了什么?
df['tenure'].astype(int)
df['ContractEnding'] = 0
for idx,val in enumerate(df['Contract']):
if df.iloc[idx]['Contract'] == 'Two year' and df.iloc[idx]['tenure']%24 in [23,0,1]:
df.iloc[idx]['ContractEnding'] = 1
## Print statement here prints fine
elif df.iloc[idx]['Contract'] == 'One year' and df.iloc[idx]['tenure']%12 in [11,0,1]:
df.iloc[idx]['ContractEnding'] = 1
else:
df.iloc[idx]['ContractEnding'] = 0
解决方案
推荐阅读
- ios - 如何使用 AFNetworking 检测删除状态?
- python - 数据框列中的时间差
- java - JPEG如何在解码文件流时跳过用户定义的标签
- php - SyntaxError:意外的令牌 < 在 JSON 中的位置 0 php
- android - 可移动的浮动操作按钮
- javascript - 如何使图像指向一个按钮(我有多个版本,每个版本都指向一个特定的按钮)
- jquery - 我想根据使用jQuery向下拉框动态添加的类向下拉框的父级添加一个类
- r - 对于 R 数据帧,在 dplyr 行中减去两个字符串
- python-3.x - sklearn 中的交叉验证:我需要调用 fit() 和 cross_val_score() 吗?
- datagrid - 在 JBOSS 数据网格远程缓存中执行模糊查询