Reputation: 51
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
Upvotes: 0
Views: 153
Reputation: 1040
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"
Upvotes: 1