熊猫的多条件分配

时间:2020-04-01 14:53:19

标签: pandas

我如何向量化类似

if col > 1:
    col = 5
elif col < -1:
    col = 10
else:
    col = 15

我想在熊猫里做

df['col'] = ...?

1 个答案:

答案 0 :(得分:1)

您可以为此使用np.select。下面的示例:

conditions = [
    (df['col'] > 1),
    (df['col'] < -1)]
choices = [5, 10]
df['col'] = np.select(conditions, choices, default=15)
相关问题