首页 > 其他 > 详细

NumPy array boolean indexing

时间:2020-01-31 19:08:16      阅读:68      评论:0      收藏:0      [点我收藏+]

原创转载请注明出处:https://www.cnblogs.com/agilestyle/p/12246045.html

 

Boolean Indexing

The boolean array must be of the same length as the array axis it’s indexing. 

Selecting data from an array by boolean indexing always creates a copy of the data, even if the returned array is unchanged.

技术分享图片

 

select from the rows where names == ‘Bob‘ and index the columns

技术分享图片

 

select everything but ‘Bob‘, you can either use != or negate the condition using ~

技术分享图片

 

select two of the three names to combine multiple boolean conditions, use boolean arithmetic operators like & (and) and | (or)

技术分享图片

Note: The Python keywords and and or do not work with boolean arrays. Use & (and) and | (or) instead.

 

Setting values with boolean arrays works in a common-sense way. To set all of the negative values in data to 0 we need only do

技术分享图片

 

Setting whole rows or columns using a one-dimensional boolean array is also easy

技术分享图片

 

Reference

Python for Data Analysis Second Edition

NumPy array boolean indexing

原文:https://www.cnblogs.com/agilestyle/p/12246045.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!