Numpy where with multiple conditions
You can use a ternary:
np.where(consumption_energy > 400, 'high',
(np.where(consumption_energy < 200, 'low', 'medium')))
Putting Multiple conditions using np.where on python pandas?
Try:
load['print'] = load.apply(lambda x:(x.B>x.C)&(x.C==x.D)&(x.C!=0)&(x.D!=0),axis=1)
A B C D E F print
0 a 4 7 1 5 a False
1 b 5 8 3 3 a False
2 c 4 9 5 6 a False
3 d 5 4 4 9 b True
4 e 5 2 2 2 b True
5 f 4 0 0 4 b False
or
load['print'] = np.where((load.B>load.C)&(load.C==load.D)&(load.C!=0)&(load.D!=0),True, False)
Python multiple conditions with numpy.where
You are close, ()
are missing because priority of operators:
dfe['period'] = np.where((dfe['Time'] >= "07:30:00.000") &
(dfe['Time'] <= "10:00:00.000") , '1', '2')
Another solution with between
:
dfe['period'] = np.where(dfe['Time'].between("07:30:00.000", "10:00:00.000") , '1', '2')
Using numpy.where function with multiple conditions but getting valueError
You should use bitwise &
and parantheses, rather than and
.
df['R'] = numpy.where((df['H'] > df['T']) & (df['P'] > 0),
df['C'] / df['T'] - 1, 0)
Numpy where function multiple conditions
The best way in your particular case would just be to change your two criteria to one criterion:
dists[abs(dists - r - dr/2.) <= dr/2.]
It only creates one boolean array, and in my opinion is easier to read because it says, is dist
within a dr
or r
? (Though I'd redefine r
to be the center of your region of interest instead of the beginning, so r = r + dr/2.
) But that doesn't answer your question.
The answer to your question:
You don't actually need where
if you're just trying to filter out the elements of dists
that don't fit your criteria:
dists[(dists >= r) & (dists <= r+dr)]
Because the &
will give you an elementwise and
(the parentheses are necessary).
Or, if you do want to use where
for some reason, you can do:
dists[(np.where((dists >= r) & (dists <= r + dr)))]
Why:
The reason it doesn't work is because np.where
returns a list of indices, not a boolean array. You're trying to get and
between two lists of numbers, which of course doesn't have the True
/False
values that you expect. If a
and b
are both True
values, then a and b
returns b
. So saying something like [0,1,2] and [2,3,4]
will just give you [2,3,4]
. Here it is in action:
In [230]: dists = np.arange(0,10,.5)
In [231]: r = 5
In [232]: dr = 1
In [233]: np.where(dists >= r)
Out[233]: (array([10, 11, 12, 13, 14, 15, 16, 17, 18, 19]),)
In [234]: np.where(dists <= r+dr)
Out[234]: (array([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]),)
In [235]: np.where(dists >= r) and np.where(dists <= r+dr)
Out[235]: (array([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]),)
What you were expecting to compare was simply the boolean array, for example
In [236]: dists >= r
Out[236]:
array([False, False, False, False, False, False, False, False, False,
False, True, True, True, True, True, True, True, True,
True, True], dtype=bool)
In [237]: dists <= r + dr
Out[237]:
array([ True, True, True, True, True, True, True, True, True,
True, True, True, True, False, False, False, False, False,
False, False], dtype=bool)
In [238]: (dists >= r) & (dists <= r + dr)
Out[238]:
array([False, False, False, False, False, False, False, False, False,
False, True, True, True, False, False, False, False, False,
False, False], dtype=bool)
Now you can call np.where
on the combined boolean array:
In [239]: np.where((dists >= r) & (dists <= r + dr))
Out[239]: (array([10, 11, 12]),)
In [240]: dists[np.where((dists >= r) & (dists <= r + dr))]
Out[240]: array([ 5. , 5.5, 6. ])
Or simply index the original array with the boolean array using fancy indexing
In [241]: dists[(dists >= r) & (dists <= r + dr)]
Out[241]: array([ 5. , 5.5, 6. ])
Related Topics
Importerror: No Module Named Psycopg2 After Install
How to Download Multiple Files or an Entire Folder from Google Colab
Pandas Fill in Missing Date Within Each Group With Information in the Previous Row
Best Practice to Run Multiple Spark Instance At a Time in Same Jvm
How to Clean \Xc2\Xa0 \Xc2\Xa0..... in Text Data
How to Make My Discord.Py Bot Play Mp3 in Voice Channel
How to Insert a Word into a List in Python
Expression to Remove Url Links from Twitter Tweet
Importing Large Tab-Delimited .Txt File into Python
How to Insert a Checkbox in a Django Form
Sqlalchemy, Prevent Duplicate Rows
Opencv Typeerror: Expected Cv::Umat for Argument 'Src' - What Is This
Best Way to Identify and Extract Dates from Text Python