np.vectorize np.piecewise 用法小结

np.vectorize

ladders = {1:11, 2:22, 3:33}
ladder_move = np.vectorize(lambda x: ladders[x] if x in ladders else x)
print(ladder_move)
ladder_move(1)
ladder_move(2)
ladder_move(3)
ladder_move(4)
ladder_move(np.arange(50))

result:


[ 0 11 22 33  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
 48 49]

np.piecewise

#%%
step2 = np.arange(90,110)
step22 = np.piecewise(step2, [step2 > 100, step2 <= 100],
    [lambda x: 200 - x, lambda x: x])
print(step2)
print(step22)

result:

[ 90  91  92  93  94  95  96  97  98  99 100 101 102 103 104 105 106 107
 108 109]
[ 90  91  92  93  94  95  96  97  98  99 100  99  98  97  96  95  94  93
  92  91]

你可能感兴趣的:(数据分析包使用,Python数据分析)