5
data['BUILDING CLASS CATEGORY'] = np.where(data['BUILDING CLASS 
CATEGORY']!='01 ONE FAMILY DWELLINGS' or '02 TWO FAMILY 
DWELLINGS ', 'OTHERS' , data['BUILDING CLASS CATEGORY'])

neither

data['BUILDING CLASS CATEGORY'] = np.where(data['BUILDING CLASS 
CATEGORY']!='01 ONE FAMILY DWELLINGS' or data['BUILDING 
CLASS CATEGORY']!='02 TWO FAMILY DWELLINGS', 'OTHERS' , 
data['BUILDING CLASS CATEGORY'])

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

R-Yue
  • 85
  • 2
  • 2
  • 8
  • 2
    Possible duplicate of [Numpy where function multiple conditions](https://stackoverflow.com/questions/16343752/numpy-where-function-multiple-conditions) – Kaushik NP Oct 10 '17 at 09:48
  • With & for `and` and ^ for `or` – foxyblue Oct 10 '17 at 09:57
  • And wrap the comparisons in () so they evaluated first, e.g. `(a<0) | (b>3)`. – hpaulj Oct 10 '17 at 16:40
  • Friendly suggestion: formatting would make this question much more readable. For instance, the long strings with capitals and spaces in them make it super hard to read, and make things go to the next line. It is really rough. Readability matters for questions! Also, all caps is inherently annoying. :) – eric Jan 26 '22 at 14:27

2 Answers2

9

Your second try is very close, using numpy.where and note that [its] conditional statement uses bitwise operators (& | ^ << >> ~).
Put everything together we'll have the following;

import pandas as pd
import numpy as np

data = pd.DataFrame({'COL': ['01 thing','02 thing','03 thing']})

print(data)
>>>    COL
>>> 0  01 thing
>>> 1  02 thing
>>> 2  03 thing

data['COL'] = np.where((data['COL'] != '01 thing') | 
                       (data['COL'] != '02 thing'), 'other', data['COL'])

print(data)
>>>    COL
>>> 0  other
>>> 1  other
>>> 2  other

(suggestion:) If you want to replace all records that is not '01 thing' and not '02 thing' you might want to replace | with & instead. Also, I would consider using str.startswith.
Substituting that into your np.where(condition) we have;

data['COL'] = np.where(~data['COL'].str.startswith('01') &
                       ~data['COL'].str.startswith('02'), 'other', data['COL'])

print(data)
>>>    COL
>>> 0  01 thing
>>> 1  other
>>> 2  02 thing
fielc92
  • 815
  • 10
  • 12
0

To get np.where() working with multiple conditions, do the following:

np.where((condition 1) & (condition 2)) # for and
np.where((condition 1) | (condition 2)) # for or

Why do we have do to things this way (with parentheses and & instead of and)? I'm not 100% sure, frankly, but see the very long discussions of this question at this post.

eric
  • 7,142
  • 12
  • 72
  • 138