data['BUILDING CLASS CATEGORY'] = np.where(data['BUILDING CLASS
CATEGORY']!='01 ONE FAMILY DWELLINGS' or '02 TWO FAMILY
DWELLINGS ', 'OTHERS' , data['BUILDING CLASS CATEGORY'])
既不
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:系列的真值是不明确的。使用a.empty,a.bool(),a.item(),a.any()或a.all()。
答案 0 :(得分:2)
您的第二次尝试非常接近,使用numpy.where
并注意[其]条件语句使用bitwise operators(& | ^ << >> ~
)。
把所有东西放在一起,我们将有以下内容;
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
(建议)如果您要替换非'01 thing'
而非'02 thing'
的所有记录,您可能希望将|
替换为{{1}相反。另外,我会考虑使用str.startswith
将其替换为我们的&
;
np.where(condition)