>>> a = np.array([[1,2,3,4],[5,6,7,8],[9,10,11,12]])
>>> a
array([[ 1, 2, 3, 4],
[ 5, 6, 7, 8],
[ 9, 10, 11, 12]])
>>> a[a[:,0] > 3] # select rows where first column is greater than 3
array([[ 5, 6, 7, 8],
[ 9, 10, 11, 12]])
>>> a[a[:,0] > 3][:,np.array([True, True, False, True])] # select columns
array([[ 5, 6, 8],
[ 9, 10, 12]])
# fancier equivalent of the previous
>>> a[np.ix_(a[:,0] > 3, np.array([True, True, False, True]))]
array([[ 5, 6, 8],
[ 9, 10, 12]])
For an explanation of the obscure np.ix_(), see https://stackoverflow.com/a/13599843/4323
Finally, we can simplify by giving the list of column numbers instead of the tedious boolean mask:
>>> a[np.ix_(a[:,0] > 3, (0,1,3))]
array([[ 5, 6, 8],
[ 9, 10, 12]])
Answer from John Zwinck on Stack Overflowpython - Extracting specific columns in numpy array by condition - Stack Overflow
python - Numpy select rows based on condition - Stack Overflow
numpy - Selecting rows if column values meet certain condition - Stack Overflow
python - Numpy Select with Multiple Conditions Not Returning Values - Stack Overflow
>>> a = np.array([[1,2,3,4],[5,6,7,8],[9,10,11,12]])
>>> a
array([[ 1, 2, 3, 4],
[ 5, 6, 7, 8],
[ 9, 10, 11, 12]])
>>> a[a[:,0] > 3] # select rows where first column is greater than 3
array([[ 5, 6, 7, 8],
[ 9, 10, 11, 12]])
>>> a[a[:,0] > 3][:,np.array([True, True, False, True])] # select columns
array([[ 5, 6, 8],
[ 9, 10, 12]])
# fancier equivalent of the previous
>>> a[np.ix_(a[:,0] > 3, np.array([True, True, False, True]))]
array([[ 5, 6, 8],
[ 9, 10, 12]])
For an explanation of the obscure np.ix_(), see https://stackoverflow.com/a/13599843/4323
Finally, we can simplify by giving the list of column numbers instead of the tedious boolean mask:
>>> a[np.ix_(a[:,0] > 3, (0,1,3))]
array([[ 5, 6, 8],
[ 9, 10, 12]])
If you do not want to use boolean positions but the indexes, you can write it this way:
A[:, [0, 2, 3]][A[:, 1] == i]
Going back to your example:
>>> A = np.array([[1,2,3,4],[6,1,3,4],[3,2,5,6]])
>>> print A
[[1 2 3 4]
[6 1 3 4]
[3 2 5 6]]
>>> i = 2
>>> print A[:, [0, 2, 3]][A[:, 1] == i]
[[1 3 4]
[3 5 6]]
Seriously,
Use a boolean mask:
mask = (z[:, 0] == 6)
z[mask, :]
This is much more efficient than np.where because you can use the boolean mask directly, without having the overhead of converting it to an array of indices first.
One liner:
z[z[:, 0] == 6, :]
Program:
import numpy as np
np_array = np.array([[0,4],[0,5],[3,5],[6,8],[9,1],[6,1]])
rows=np.where(np_array[:,0]==6)
print(np_array[rows])
Output:
[[6 8]
[6 1]]
And If You Want to Get Into 2d List use
np_array[rows].tolist()
Output of 2d List
[[6, 8], [6, 1]]
Try setting emailconditions like so:
emailconditions = [
dfFinal['EmailC'].notna(),
dfFinal['EmailC'].isna() & dfFinal['EmailB'].notna(),
dfFinal['EmailC'].isna() & dfFinal['EmailB'].isna()]
Key point is to use notna() instead of is not None and isna() instead of is None.
Here's an alternative solution, which grabs the first truthy value out of the columns in whichever priority order you want:
In [3]: df
Out[3]:
a b c
0 x w None
1 None y None
2 k None z
In [4]: order = ("c", "b", "a")
In [5]: df.apply(lambda row: next(row[col] for col in order if row[col]), axis=1)
Out[5]:
0 w
1 y
2 z
dtype: object
If you anticipate having rows where none of the columns have a value, then you'd probably want something like this:
def first_truthy(row, order):
try:
return next(row[col] for col in order if row[col])
except StopIteration:
return None
Output:
In [7]: df
Out[7]:
a b c
0 x w None
1 None y None
2 k None z
3 None None None
In [8]: df.apply(lambda row: first_truthy(row, order), axis=1)
Out[8]:
0 w
1 y
2 z
3 None
dtype: object
Not that this is likely slower than boolean masking, but (in my opinion) easier to reason about and debug, and doesn't require the extra dependency on numpy. If you need performance, @richardec's solution is likely vastly superior, though I have not benchmarked our solutions.