Questions tagged [boolean-indexing]
27 questions
89
votes
4 answers
Getting a list of indices where pandas boolean series is True
I have a pandas series with boolean entries. I would like to get a list of indices where the values are True.
For example the input pd.Series([True, False, True, True, False, False, False, True])
should yield the output [0,2,3,7].
I can do it with…

James McKeown
- 1,284
- 1
- 9
- 14
9
votes
1 answer
Explanation of boolean indexing behaviors
For the 2D array y:
y = np.arange(20).reshape(5,4)
---
[[ 0 1 2 3]
[ 4 5 6 7]
[ 8 9 10 11]
[12 13 14 15]
[16 17 18 19]]
All indexing select 1st, 3rd, and 5th rows. This is clear.
print(y[
[0, 2, 4],
::
])
print(y[
[0, 2, 4],
…

mon
- 18,789
- 22
- 112
- 205
3
votes
1 answer
Pandas Boolean Filter with Assignment resulting in NaN
I'm curious why this toy example for simultaneous boolean index + assignment in Pandas doesn't work:
df = pd.DataFrame({'Source': ['A', 'B', 'C', 'A', 'B', 'C'],
'Period': ['1 hr', '1 hr', '1 hr', '24 hr', '24 hr', '24 hr'],
…

Arthur Lin
- 85
- 4
3
votes
1 answer
Numpy boolean index assignment sometimes fails and assigns entire array
I would like to simply assign a label to each element of an array based on it being below or above a certain threshold and solve this with boolean indexing:
def easy_labeling(arr, thresh=5):
negative_mask = arr < thresh
positive_mask = arr >=…

phil
- 31
- 1
3
votes
2 answers
Python Numba jit function with if statement
I have a piecewise function with 3 parts that I'm trying to write in Python using Numba @jit instruction. The function is calculated over an array. The function is defined by:
@njit(parallel=True)
def f(x_vec):
N=len(x_vec)
…

dani
- 147
- 2
- 10
3
votes
1 answer
Received 'Boolean Series key will be reindexed to match DataFrame index' warning when creating a new data frame
Is there any potential downside to using the following code to create a new data frame, wherein I'm specifying very specific information from the original data frame I want to see in the new data frame.
df_workloc = (df[df['WorkLoc'] ==…

Tweep
- 71
- 1
- 2
- 9
2
votes
3 answers
Python/Numpy/Boolean Indexing: Modify boolean array at locations with N consecutive True values
Given a numpy boolean array
arr = np.array([1, 0, 1, 1, 0, 0, 1, 1, 1, 0, 1])
I would like to indicate where there are at least n consecutive true values (from left to right).
For n = 2:
# True 2x (or more) in a row
# / \ …

Craig Nathan
- 197
- 10
1
vote
1 answer
Using df.loc[] vs df[] shorthand with boolean masks, pandas
Both df[booleanMask] and df.loc[booleanMask] are working for me but I don't understand why. The shorthand df[] without using .loc I thought applied to the column whereas I am trying to apply to the row, so I thought I needed to use .loc
Here is the…

Deano
- 13
- 3
1
vote
2 answers
Equivalent to Pandas boolean indexing for xarray?
I have an xarray dataset that I would like to index using boolean indexing as I would with Pandas. For example, I have a pandas dataframe:
A B C
5 1 0
4 1 0
3 5 0
4 3 0
5 2 0
And I want only rows where the value in B is greater than 2, like…

Billiam
- 165
- 1
- 13
1
vote
1 answer
Efficient chaining of boolean indexers in pandas DataFrames
I am trying to very efficiently chain a variable amount of boolean pandas Series, to be used as a filter on a DataFrame through boolean indexing.
Normally when dealing with multiple boolean conditions, one chains them like this
condition_1 = (df.A >…

Wouter
- 160
- 8
1
vote
1 answer
Python/Numpy/Boolean Indexing: For each True value in array, modify the next 2 elements
I have a numpy array
array = np.array([5,100,100,100,5,5,100,100,100,5])
I create a mask with boolean indexing like so:
mask = (array < 30)
This gives a mask like
[ True False False False True True False False False True]
I can get the indices…

Craig Nathan
- 197
- 10
1
vote
1 answer
How do I index an pandas dataframe using boolean indexing?
I am starting a new practice module in pandas where we deal with indexing and filtering of data. I have come across a format of method chaining that was not explained in the course and I was wondering if anyone could help me make sense of this. The…

Oscar Agbor
- 71
- 8
1
vote
1 answer
Python effect of boolean values as index (a[a==0] = 1)
I am currently implementing some code I have seen on github.
(https://gist.github.com/karpathy/a4166c7fe253700972fcbc77e4ea32c5)
The point of interest here is the following:
def prepro(I):
""" prepro 210x160x3 uint8 frame into 6400 (80x80) 1D
…

Dubbox
- 191
- 1
- 2
- 20
1
vote
2 answers
Filter numpy array but keep original value
In other words, does numpy support 'sieving'?
I have two arrays:
a = np.array([1, 0, 2, 3, 0, 4])
b = np.array([1, 0, 0, 0, 0, 6])
What I want is to return a new array, c, that contains the original values of a based on the mask of b:
c = a[b > 0]…

Drivebyluna
- 344
- 2
- 14
0
votes
1 answer
Behavior of np.take with Boolean arrays
np.take is a function that takes the elements from an array along an axis. When there is an axis parameter specified it behaves exactly like "fancy" indexing (indexing using arrays), but without an axis parameter the array a is flattened, and then…

Karl Gardner
- 7
- 1