python - Index Pandas with multiple boolean arrays - Stack Overflow

Using numpy, one can subset an array with one boolean array per dimension like:In [10]: aa = np.array(

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

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744851615a4597158.html

相关推荐

  • python - Index Pandas with multiple boolean arrays - Stack Overflow

    Using numpy, one can subset an array with one boolean array per dimension like:In [10]: aa = np.array(

    2天前
    60

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信