Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python multiple conditions with numpy.where [duplicate]

Tags:

python

pandas

I am trying to do some simple manipulation of a pandas dataframe. I have imported pandas as pd and numpy as np and imported a csv to create a dataframe called 'dfe'.

I have had success with the following code to populate a new column based on one condition:

dfe['period'] = np.where(dfe['Time'] >= "07:30:00.000" , '1', '2')

But when I try to use a similar technique to populate the same column based on two conditions, I get an error (unsupported operand type(s) for &: 'bool' and 'str')

Here is my attempt at the multiple condition version:

dfe['period'] = np.where(dfe['Time'] >= "07:30:00.000" & dfe['Time'] <= "10:00:00.000" , '1', '2')

I have had a look at lots of solutions to similar problems but they are all a little bit too complicated for me to understand given I have just started and was hoping someone could give me some clues about why this is not working.

Thanks

like image 521
Mark D Avatar asked Sep 10 '26 20:09

Mark D


1 Answers

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')
like image 62
jezrael Avatar answered Sep 13 '26 10:09

jezrael



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!