我如何向量化类似
if col > 1:
col = 5
elif col < -1:
col = 10
else:
col = 15
我想在熊猫里做
df['col'] = ...?
答案 0 :(得分:1)
您可以为此使用np.select
。下面的示例:
conditions = [
(df['col'] > 1),
(df['col'] < -1)]
choices = [5, 10]
df['col'] = np.select(conditions, choices, default=15)