Select rows based on a condition in numpy/python
Try using np.where
combined with np.sum
:
import numpy as np
np.random.seed(0)
a = np.random.randn(4, 4)
indices = np.where(np.sum(a, axis=1) > 0)
print(a[indices]) # rows with sum > 0
Python numpy filter two-dimensional array by condition
You can use a bool
index array that you can produce using np.in1d
.
You can index a np.ndarray
along any axis
you want using for example an array of bool
s indicating whether an element should be included. Since you want to index along axis=0
, meaning you want to choose from the outest index, you need to have 1D np.array
whose length is the number of rows. Each of its elements will indicate whether the row should be included.
A fast way to get this is to use np.in1d
on the second column of a
. You get all elements of that column by a[:, 1]
. Now you have a 1D np.array
whose elements should be checked against your filter. Thats what np.in1d
is for.
So the complete code would look like:
import numpy as np
a = np.asarray([[2,'a'],[3,'b'],[4,'c'],[5,'d']])
filter = np.asarray(['a','c'])
a[np.in1d(a[:, 1], filter)]
or in a longer form:
import numpy as np
a = np.asarray([[2,'a'],[3,'b'],[4,'c'],[5,'d']])
filter = np.asarray(['a','c'])
mask = np.in1d(a[:, 1], filter)
a[mask]
Subsetting 2D array based on condition in numpy python
You can use np.where to preserve the shape:
np.where(arr_b > 0.0, arr_a, np.nan)
It will take the corresponding values from arr_a when arr_b's value is greater than 0, otherwise it will use np.nan.
import numpy as np
N = 5
arr_a = np.random.randn(N,N)
arr_b = np.random.randn(N,N)
np.where(arr_b > 0.0, arr_a, np.nan)
Out[107]:
array([[ 0.5743081 , nan, -1.69559034, nan, 0.4987268 ],
[ 0.33038264, nan, -0.27151598, nan, -0.73145628],
[ nan, 0.46741932, 0.61225086, nan, 1.08327459],
[ nan, -1.20244926, 1.5834266 , -0.04675223, -1.14904974],
[ nan, 1.20307104, -0.86777899, nan, nan]])
how to slice 2D numpy array based on 2 1D arrays containing initial and final indexes
You can use numpy.split
with flattened data
(using numpy.ndarray.flatten
) and modifying the slices:
sections = np.column_stack([initial, final]).flatten()
sections[::2] += np.arange(len(initial)) * data.shape[1]
sections[1::2] += sections[::2] - np.array(initial)
np.split(data.flatten(), sections)[1::2]
finding array rows that satisfy conditions on two column values
You could use vector algebraic operations. I am not sure if it simplifies but it is funky though.
arr[np.prod(arr[...,1:3] < k, axis=1) + np.prod(arr[...,1:3] > k, axis=1) > 0,:]
The logic behind is to check both columns if they are lower (or bigger) than k
and multiply the results which corresponds to an and
and then add up the resulting vectors and check if they are bigger than 1 which should correspond to the or
.
Conditional maths operation on 2D numpy array checking on one dimension and doing different operations on diff dimensions
I would calculate a mask, or boolean index, and use if for each column:
Construct a sample array:
pantilt=np.column_stack([np.linspace(-180,180,11),np.linspace(0,90,11)])
I = pantilt[:,0]>90
# J = pantilt[:,0]<-90
pantilt[I,0] -= 180
pantilt[I,1] *= -1
I = pantilt[:,0]<-90 # could use J instead
pantilt[I,0] += 180
pantilt[I,1] *= -1
Before:
array([[-180., 0.],
[-144., 9.],
[-108., 18.],
[ -72., 27.],
[ -36., 36.],
[ 0., 45.],
[ 36., 54.],
[ 72., 63.],
[ 108., 72.],
[ 144., 81.],
[ 180., 90.]])
After:
array([[ 0., -0.],
[ 36., -9.],
[ 72., -18.],
[-72., 27.],
[-36., 36.],
[ 0., 45.],
[ 36., 54.],
[ 72., 63.],
[-72., -72.],
[-36., -81.],
[ 0., -90.]])
This would work just as well if the columns were separate 1d arrays.
How to numpy-ify a two dimensional conditional lookup?
You can use a boolean mask for indexing the Sx
and prepped_array
and then use two index arrays, derived from the prepped_array
, to index into the M
array. The code might speak clearer than the previous sentence:
mask = prepped_array[:, :, 0] != -1
Sx[mask] = M[tuple(prepped_array[mask].T)]
Let's look at the involved steps:
mask = prepped_array[:, :, 0] != -1
creates a 2D boolean array indicating where the condition is met.prepped_array[mask]
creates a 2D array where entries from the previous 3rd dimensions appear now along the 2nd dimension; the first dimensions corresponds to eachTrue
instance inmask
.tuple(prepped_array[mask].T)
creates two 1D arrays which can be used to further index into other arrays: the first array denotes row indices and the second array denotes column indices.- So
Sx[mask] = M[tuple(prepped_array[mask].T)]
maps the indices contained inprepped_array
to the arrayM
using the previous two 1D index arrays. Sx[mask]
finally references those elements inSx
that for which the condition inprepped_array[:, :, 0]
is met.
Filter a 2D numpy array
You should perform the condition only over the first column:
x_displayed = xy_dat[((xy_dat[:,0] > min) & (xy_dat[:,0] < max))]
What we do here is constructing a view where we only take into account the first column with xy_dat[:,0]
. By now checking if this 1d is between bounds, we construct a 1D boolean array of the rows we should retain, and now we make a selection of these rows by using it as item in the xy_dat[..]
parameter.
Related Topics
How to Make an Auto Increment Integer Field in Django
Determining Neighbours of Cell Two Dimensional List
How to Write to an Existing Excel File Without Overwriting Data (Using Pandas)
Removing White Space Around a Saved Image
Python - How to Separate Paragraphs from Text
What Is the Correct Way to Make My Pyqt Application Quit When Killed from the Console (Ctrl-C)
Pandas: Update Column Values from Another Column If Criteria
Typeerror: Missing 1 Required Positional Argument: 'Self'
How to Convert Column With String Type to Int Form in Pyspark Data Frame
What Causes a Python Segmentation Fault
How to Convert Signed to Unsigned Integer in Python
Convert HTML String to an Image in Python
Changing Presence Discord Status
Reading Columns of a Txt File on Python
How to Merge Json Objects Containing Arrays Using Python
How to Write Multiple Images (Subplots) into One Image
How to Assign Class Instance to a Variable and Use That in Other Class