How to use two condition in np.where
1 2 3 | data['BUILDING CLASS CATEGORY'] = np.where(data['BUILDING CLASS CATEGORY']!='01 ONE FAMILY DWELLINGS' or '02 TWO FAMILY DWELLINGS ', 'OTHERS' , data['BUILDING CLASS CATEGORY']) |
也不
1 2 3 4 | 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']) |
值错误:序列的真值不明确。使用a.empty、a.bool()、a.item()、a.any()或a.all()。
第二次尝试非常接近,使用
把所有的东西放在一起,我们会有以下内容:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | 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 |
(建议:)如果您要替换所有不是
我们已经把它代入你的
1 2 3 4 5 6 7 8 | 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 |