[英]Create a new variable on condition of two other variables
我在 dataframe 标签中有两个二进制变量:
target1=[0,1,0,0,0.....1]
target2=[1,1,0,0,0.....0]
我想创建第三个变量:
- 如果目标 1=0 和目标 2=0,T=0,
- 如果目标 1=1 和目标 2=0,T=1,
- 如果目标 1=0 和目标 2=1,T=2,
- 如果目标 1=1 且目标 2=1,则 T=3。
for i in range(len(labels)):
if target1[i]==0 and target2[i]==0:
labels['T']=0
elif target1[i]==1 and target2[i]==0:
labels['T']=1
elif target1[i]==0 and target2[i]==1:
labels['T']=2
else:
labels['T']=3
出于某种原因,唯一的结果是 0。我不确定出了什么问题。
可能你需要改变
for i in range(len(labels)):
至
for i in range(len(target1)):
否则,您只运行一次循环或标签中有任何项目
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.