最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

python - Index Pandas with multiple boolean arrays - Stack Overflow

programmeradmin3浏览0评论

Using numpy, one can subset an array with one boolean array per dimension like:

In [10]: aa = np.array(range(9)).reshape(-1, 3)

In [11]: aa
Out[11]:
array([[0, 1, 2],
       [3, 4, 5],
       [6, 7, 8]])

In [12]: conditions = (np.array([True, True, False]), np.array([True, False, True]))

In [13]: aa[np.ix_(*conditions)]
Out[13]:
array([[0, 2],
       [3, 5]])

Is there a way to do this in Pandas? I've looked in their docs
.html#boolean-indexing
but didn't find it. (I would have posted 4 relevant links, but then the automatic question checks think I've posted code that is not properly formatted.)

This

github issue is close, but I want to pick entire rows and columns.

Using numpy, one can subset an array with one boolean array per dimension like:

In [10]: aa = np.array(range(9)).reshape(-1, 3)

In [11]: aa
Out[11]:
array([[0, 1, 2],
       [3, 4, 5],
       [6, 7, 8]])

In [12]: conditions = (np.array([True, True, False]), np.array([True, False, True]))

In [13]: aa[np.ix_(*conditions)]
Out[13]:
array([[0, 2],
       [3, 5]])

Is there a way to do this in Pandas? I've looked in their docs
https://pandas.pydata./docs/user_guide/indexing.html#boolean-indexing
but didn't find it. (I would have posted 4 relevant links, but then the automatic question checks think I've posted code that is not properly formatted.)

This
https://github/pandas-dev/pandas/issues/11290
github issue is close, but I want to pick entire rows and columns.

Share Improve this question asked Mar 10 at 10:37 AntonAnton 4493 silver badges14 bronze badges 1
  • @ouroboros1 no need to unpack, a tuple should work fine – mozway Commented Mar 10 at 10:49
Add a comment  | 

1 Answer 1

Reset to default 3

You should be able to directly use boolean indexing with iloc (or loc):

df = pd.DataFrame(aa)
out = df.iloc[conditions]

Note that conditions should be a tuple of arrays/lists/iterables, if not you should convert it:

out = df.iloc[tuple(conditions)]

Output:

   0  2
0  0  2
1  3  5
发布评论

评论列表(0)

  1. 暂无评论