2016-11-20 47 views
1

我想基於不同列的內容在指定索引添加一列:蟒蛇大熊貓基於條件語句添加欄目內容

df.insert(10, 'Escalation Status', np.where(df['Reasons for escalation'] == '', 'FALSE', 'TRUE')) 

所以,當柱「的升級理由」不能爲空,寫入「FALSE」,如果不是,則在新創建的「升級狀態」列中寫入「TRUE」。

我的問題:無論「升級原因」字段中是否有任何內容,內容始終爲「真」。

輸出示例:

Country,Date,Vendor,Product Family,Case number,Reseller Name,OpeningDate,Closing Date,Case Status,Topic,Escalation Reason,Reasons for escalation 
DEU,28.10.2016,VENDOR,,201610281078864,,28.10.2016,28.10.2016,closed,Software issue,TRUE,Software 
DEU,28.10.2016,VENDOR,,201610281078862,,28.10.2016,28.10.2016,closed,Config help,TRUE, 

第一行是正確的(理由升級,是不是空的,它包含「軟件」),但第二行應該是「假」,爲理由升級是空

感謝

回答

1

我認爲你需要isnull的比較,如果NaN值:

df.insert(10, 'Escalation Status', 
      np.where(df['Reasons for escalation'].isnull(), 'FALSE', 'TRUE')) 
+0

這樣做,非常感謝。 – f0rd42

+1

很高興能幫到你! – jezrael