簡體   English   中英

根據條件將 boolean 值分配給新列

[英]Assigning boolean value to a new column based on conditions

我需要根據名為X ( 1,2,3,4 , 5 ) 的列的值將 boolean 值分配給新列Y中的行。 我在數據集df中有此列:

X
1
1
1
3
2
5
2
4
1

我想要一個新的 Y,在一個新的數據集中,它是 df 的副本,其中:

  • 如果行的 X 值 = 1,則為 True
  • 如果行的 X 值 = 2,則為 False
  • 如果行的 X 值 = 3,則為 False
  • 如果行的 X 值 = 4,則為 True
  • 如果行的 X 值 = 5,則為 False

所以我應該有

X        Y
1      true
1      true
1      true
3      false
2      false
5      false
2      false
4      true
1      true

我寫了這段代碼:

new_df=df.copy()
new_df['Y'] = False
for index in df.iterrows():
    if   df['X'] == 1:
        new_df.iloc[index,9] = True
    elif df['X'] == 2:
        new_df.iloc[index,9] = False
    elif df['X'] == 3:
        new_df.iloc[index,9] = False
    elif df['X'] == 4:
        new_df.iloc[index,9] = True
    else:
        new_df.iloc[index,9] = False

收到此錯誤:

ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().

你能幫我修復代碼以獲得預期的 output 嗎? 謝謝

編輯: np.where() 優於 map()

我相信您需要做的是創建一個自定義 function ,您可以在其中使用if-elif-else然后使用map 類似於以下內容:

def evaluator(x):
   if x == 1:
      return True
   elif x == 2:
      return False
   elif x == 3:
      return False
   elif x == 4: 
      return True
   else:
      return False
df['Y'] = df['X'].map(lambda x: evaluator(x))

@Allolz 注釋提供了有用的簡化,它還可以允許使用帶有np.where()的矢量化操作

df['Y'] = np.where(df['X'].isin([1,4]),True,False) 

在您的情況下,根據您的輸入 dataframe,輸出:

   X      Y
0  1   True
1  1   True
2  1   True
3  3  False
4  2  False
5  5  False
6  2  False
7  4   True
8  1   True

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

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