我正在尝试根据另一列创建一个新列。特别是如果它包含某个值。
我已经执行以下操作:
df['region'] = np.where(df['location'].str.contains("AK| AZ | CA | CO | HI |ID | MT | NM | NV | OR | UT | WA | WY", na=False), "west",
np.where(df['location'].str.contains("PA | NJ | NY | VT | NH | MA | RI | CT | ME", na=False), "northwest",
np.where(df['location'].str.contains("AR | AL | DC | DE | FL | GA | KY | LA | MD | MS | NC | OK | SC | VA | WV", na=False), "south",
np.where(df['location'].str.contains("IA | IL | IN | KS |MI | MN |MO | ND |NE | OH | SD | WI", na=False), "midwest", "international"))))
我得到这个:
location region
Columbia, MO international
Maplewood, NJ international
预期:
location region
Columbia, MO midwest
Maplewood, NJ northwest
我基本上有一个“位置”列,我想检查它是否包含缩写之一,然后为该区域创建一个新列。
谢谢!