尝试根据某些字符串是否存在于不同的列中来填充数据框中的列。我可以使用一系列嵌套np.where语句来做到这一点,例如:
cond1=df.CollectType.str.contains('Outcrop')
cond2=df.CollectType.str.contains('Chip channel')
cond3=df.CollectType.str.contains('Rubble')
cond4=df.CollectType.str.contains('Float')
cond5=df.CollectType.str.contains('Dump')
df['R_SampleType'] = np.where(cond1, 'Outcrop', np.where(cond2,
'Chip channel', np.where(cond3,'Rubble',
np.where(cond4,'Float',
np.where(cond5,'Dump','')))))
但这似乎效率不高。因此,我正在尝试列出条件并使用以下命令调用列表:
values = ['Outcrop', 'Chip Channel','Rubble','Float','Dump']
conditions = list(map(df['CollectType'].str.contains, values))
df['R_SampleType'] = np.select(conditions, values, '')
但我得到了错误:
ValueError: invalid entry 0 in condlist: should be boolean ndarray
有什么建议么?