0

I am relatively new to python and am trying to get rows where columns have certain values.

Here is example of my code

item=mydf[mydf["Item Name"]=="Pregabalin"]
type=mydf[mydf["type"]=="Pregabalin 300mg"]
mydf[item & Strength]

However when I run this I get an error TypeError: ufunc 'bitwise_and' not supported for the input types, and the inputs could not be safely coerced to any supported types according to the casting rule ''safe''

Does anyone know why I am getting this error and what I can do just to return specific values? Any help would greatly be appreciated!

Katie
  • 45
  • 1
  • 7

1 Answers1

2

you can use "bitwise and" for masks like as follows:

item = mydf["Item Name"]=="Pregabalin"
typ = mydf["type"]=="Pregabalin 300mg"

mydf[item & typ]

or simply (in the following case we must use parentheses because of Operator precedence rules):

mydf[(mydf["Item Name"]=="Pregabalin") & (mydf["type"]=="Pregabalin 300mg")]
MaxU - stand with Ukraine
  • 205,989
  • 36
  • 386
  • 419
  • Slow day today... – cs95 Nov 26 '17 at 00:36
  • @cᴏʟᴅsᴘᴇᴇᴅ Thank you! this works great... I have another question for you how can I do this with numeric data; mydf[(mydf["Item Name"]=="Pregabalin") & (mydf["Quantity"]=="2.0")] I am receiving this error TypeError: invalid type comparison when I use the above code – Katie Nov 27 '17 at 01:18
  • @Katie Quantity may be a float. Try `mydf['Quantity'] == 2.0`. – cs95 Nov 27 '17 at 01:23
  • @cᴏʟᴅsᴘᴇᴇᴅ Thank you so much!! – Katie Nov 27 '17 at 01:31