簡體   English   中英

在其他兩個變量的條件下創建一個新變量

[英]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. 如果目標 1=0 和目標 2=0,T=0,
  2. 如果目標 1=1 和目標 2=0,T=1,
  3. 如果目標 1=0 和目標 2=1,T=2,
  4. 如果目標 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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM