df['STATE'].where( returning all recordss

We have the following code:

import pandas as pd

df = pd.read_excel("census.xlsx",names=cols,skiprows=range(1, 70), header=14)

df['REGION'] = df['STATE'].where(df['STATE'].str.contains("AL"), "SOUTH")
print(df)

and we’re getting SOUTH for all our records when we just want AL.

First of all, the where() method keeps the original value when the condition is True, and overrides the column value with other when the condition is False.
So if REGION is set to SOUTH, that means the condition is False for all rows.

Second, str.contains() expects a regex pattern. If you want a fixed value test, you could use

df['REGION'] = df['STATE'].where(~df['STATE'].isin(["AL","GA","FL"]), "SOUTH")

if the state is not AL, GA or FL pass the state name as is, if it is one of those values, set region to SOUTH