[英]How can I add a column to a dataframe with a value conditional on another dataframe?
[英]How can I add a column to a dataframe that is based on another columns value?
對於學校,我有一個DataFrame(包含多個列和行),並且我的任務是在該DataFrame中創建一個包含“ true”的新列(如果該數據框中的列的值大於某個值,則該新列如果該值小於數據框中某列的值,則應包含“ false”。 有人可以幫我嗎? (我應該在jupyter-notebook中這樣做)
嘗試:
import numpy as np
df[<new col>] = np.where(df[<value col>] > <threshold>, True, False)
但是,將<new col>
替換為<new col>
的字符串名稱,將<value col>
替換為要比較的列的字符串名稱,然后將<threshold>
替換為比較的值。
嘗試
df['flag'] = True
df.loc[df['A']>2,'flag'] = False
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.