creating new column with values specifying certain values (classifying US states)

I’m trying to classify US states into different regions (Northeast, midwest, south, west. df[‘State’] is the given column in the dataframe and I’m trying to create a new column df[‘Region’].

I wrote the following code but all the values showing up in the new column df[‘Region’] happened to be ‘northeast’ only.

Can you advise me what’s wrong with my code?

result = []
for value in df['State']:
    if value == 'CT' or 'ME' or 'MA' or 'NH' 'RI' or 'VT' or 'NJ' or 'NY' or 'PA':
        result.append('Northeast')
    elif value == 'IL' or 'IN' or 'MI' or 'OH' or 'WI' or 'IA' or 'KS' or 'MN' or 'MO' or 'NE' or 'ND' or 'SD':
        result.append('Midwest')
    elif value == 'AZ' or 'CO' or 'ID' or 'MT' or 'NV' or 'NM' or 'UT' or 'WY' or 'AK' or 'CA' or 'HI' or 'OR' or 'WA':
        result.append('West')
    else:
        result.append('South')

df['Region'] = result

Answer

or signifies that one of the preceding or one of the following expressions has to be true or truthy. None of the strings you used are falsy, so simply replace the keyword or with or value == "state here"

Leave a Reply

Your email address will not be published. Required fields are marked *