np.where python pandas

时间:2017-11-01 10:10:19

标签: python pandas numpy

我想做点什么:

if condition: 
    instruction 1 
    instruction 2
    ...
    instruction N

我是否必须多次重复np.where语句?

my_df["1"] = np.where(condition, instruction1, other)
my_df["2"] = np.where(condition, instruction2, other)
...
my_df["N"] = np.where(condition, instructionN, other)

1 个答案:

答案 0 :(得分:0)

不知道更多细节,也许是这样的:

instructions = [instruction1, instruction2, ..., instructionN]

for i in range(len(instructions)):
    my_df[str(i+1)] = np.where(condition, instructions[i], other)