Pandas: np.where with multiple conditions on dataframes

冷暖自知 提交于 2019-12-01 04:40:17

It is not clear to me what you exactly want to do when a y element is equal to zero... anyway the key point in this answer is "use np.logical_{and,not,or,xor} functions".

I think that the following, albeit formulated differently from your example, does what you want, but if I'm wrong you should be able to combine different tests to achieve what you want,

x = np.where(np.logical_or(x*y>0, y==0), x, 0)

Similar to the post by @gboffi, but more centralized for your original request according to my understanding try:

x = np.where(np.logical_and((x*y) < 0, x != 0))

or

x = np.where(((x*y) < 0) & (x != 0)))
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!