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.
- @ouroboros1 no need to unpack, a tuple should work fine – mozway Commented Mar 10 at 10:49
1 Answer
Reset to default 3You 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
评论列表(0条)