我正在尝试根据数据框('cnms_df'
中其他列的多个条件填充列['Mod_unMod']。我写了一些pseudo-code
来解释这个困难:
IF (cnms_df['CA'] = "NO"):
IF (cnms_df['I'] ="X" OR cnms_df['I']="V" OR cnms_df['I']="VE" OR cnms_df['I']="0.2 PCT ANNUAL CHANCE FLOOD HAZARD" OR cnms_df['I']="AREA NOT INCLUDED")
cnms_df['Mod_unMod'] = "UnMapped"
ELSE IF (LEFT(cnms_df['STUDY_TYPE].str[:3])="NON")
cnms_df['Mod_unMod'] ="NON"
ELSE IF (cnms_df['BJ']="X" OR cnms_df['BJ']="V" OR cnms_df['BJ']="V" OR cnms_df['BJ']="VE" OR cnms_df['BJ']="0.2 PCT ANNUAL CHANCE FLOOD HAZARD" OR cnms_df['BJ']="AREA NOT INCLUDED")
cnms_df['Mod_unMod']
ELSE IF (LEFT(BK2,3)="NON"
cnms_df['Mod_unMod']="UnMod"
ELSE:
cnms_df['Mod_unMod']="Modernized")
ELSE:
cnms_df['Mod_unMod'] = "UnMapped"
我已经应用了简单的np.where
语句,但是我不确定如何在上述级别上做到这一点。有没有办法以合乎逻辑的方式做这样的事情?
cnms_df['Mod_unMod'] = np.where((cnms_df['CA'] == 'No') & ((cnms_df['I'] ="X") | (cnms_df['I']="V") | (cnms_df['I']="VE") | (cnms_df['I']="0.2 PCT ANNUAL CHANCE FLOOD HAZARD") | (cnms_df['I']="AREA NOT INCLUDED")), "UnMapped", "Modernized")
答案 0 :(得分:1)
定义函数以生成新列中的内容 当前行为:
def fun(row):
lst = ['X', 'V', 'VE', '0.2 PCT ANNUAL CHANCE FLOOD HAZARD', 'AREA NOT INCLUDED']
if row.CA != 'NO:
return 'UnMapped'
elif row.I in lst:
return 'UnMapped'
elif row.STUDY_TYPE[0:3] == 'NON':
return 'NON'
elif row.BJ in lst:
return 'Mod_unMod'
elif row.BK2[0:3] == 'NON':
return 'UnMod'
else:
return 'Modernized'
然后将此功能应用于每一行,并将结果保存在新列中:
df['Mod_unMod'] = df.apply(fun, axis=1)