1 回答
TA贡献1818条经验 获得超7个赞
用途numpy.sign
:
data['Status'] = np.sign(data['Value'])
如果仅使用整数Series.clip
:
data['Status'] = data['Value'].clip(lower=-1, upper=1)
您使用列表理解的解决方案:
data['Status'] = [x / math.fabs(x) if x != 0 else 0 for x in data['Value']]
print (data)
date value Status
0 20201001 -100 -1.0
1 20200202 200 1.0
2 20200303 0 0.0
性能:
#300k rows
data = pd.concat([data] * 100000, ignore_index=True)
In [72]: %timeit data['Status'] = np.sign(data['Value'])
3.2 ms ± 57.8 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)
In [73]: %timeit data['Status'] = data['Value'].clip(lower=-1, upper=1)
5.99 ms ± 49.3 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)
In [74]: %timeit data['Status'] = [x / math.fabs(x) if x != 0 else 0 for x in data['Value']]
144 ms ± 483 µs per loop (mean ± std. dev. of 7 runs, 10 loops each)
添加回答
举报