np.where python pandas的多个指令



我想做这样的事情:

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)

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

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

最新更新