1

I have this line of code:

df['new_column']=df['price'][df['bathrooms']>=2 and df['bathrooms']<3]

Basically, I want to create a new column with the price of a listing (from column price), but only if the bathrooms column contains a value greater than or equal to 2, but less than three. I'm getting the error

The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().

How do I correct this code?

Thank you!

Anurag Dabas
  • 23,866
  • 9
  • 21
  • 41
  • 1
    Does this answer your question? [Truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all()](https://stackoverflow.com/questions/36921951/truth-value-of-a-series-is-ambiguous-use-a-empty-a-bool-a-item-a-any-o) – Anurag Dabas Aug 09 '21 at 15:50

2 Answers2

0

You need proper parathensis, you cannot use 'and' use ampsand (&) instead and use loc instead, try:

df['new_column']=df.loc[(df['bathrooms']>=2) & (df['bathrooms']<3), 'price']
Scott Boston
  • 147,308
  • 15
  • 139
  • 187
0

you can use this: query in pandas

df1 =df.query('bathrooms>=2 and bathrooms<3')
df1[['price']]
Salio
  • 1,058
  • 10
  • 21