Python Pandas Attributeerror: 'Str' Object Has No Attribute 'Isin'

First question here. I am attempting to use pandas to create a new column ('zone') in my dataframe based on the values of other columns in my dataframe. I am using address data.

Here is some sample data, my real dataset has 25k rows. sample_df:

Shipping City   Shipping Zip    State   DISTRICT NAME
Garden City 11530   NY  NEW YORK 2
San Francisco   94117   CA  CALIFORNIA 1
Frisco  75036   TX  TEXAS 1
Las Vegas   89139   NV  NV-UT
Beverly Hills   90212   CA  CALIFORNIA 5

here is sample criteria for the new column:

In general if in mid west or east coast I can use state to assign the zone. for NY: zone_8 TX: zone_6 For California, I want to split into 3 different zones: NorCal, Socal, and central Cal -which will also include Las Vegas and Phoenix. I can use the given 'District Name' column to divide CA only. for special cities I can use 'Shipping City'. he rest of the sample expected results are below:

Expected Result:

    Shipping City   | Shipping Zip  | State | DISTRICT NAME | Zone  
-----           | -----         | ----- | -----         | ----- 
Garden City | 11530         | NY    | NEW YORK 2    | zone_8    
San Francisco   | 94117         | CA    | CALIFORNIA 1  | zone_3    
Frisco          | 75036         | TX    | TEXAS 1       | zone_6    
Las Vegas       | 89139         | NV    | NV-UT         | zone_2    
Beverly Hills   | 90212         | CA    | CALIFORNIA 5  | zone_2    

I created lists for my expected values:

zone8_state = ['ME', 'NH', 'VT', 'MA', 'RI', 'CT', 'AA', 'HI', 'NY', 'PA', 'NJ', 'MD', 'DE',          'WV', 'OH', 'VA', 'NC', 'SC', 'GA', 'FL', 'MI']
zone7_state = ['KY', 'IN', 'IL', 'WI', 'MN', 'TN', 'AL','LA', 'MS', 'AR']
zone6_state = ['IA', 'MO', 'ND', 'SD', 'NE', 'KS', 'OK', 'TX']
zone5_state = ['MT', 'WV', 'CO', 'NM', 'ID', 'WA', 'OR']
zone4_state = ['UT', 'NV', 'AZ']
norcal = ['CALIFORNIA 1', 'CALIFORNIA 2']
socal = ['CALIFORNIA 4', 'CALIFORNIA 5']
zone3_city = ['CALIFORNIA 3', 'Las Vegas', 'Phoenix']

Then I made a function:

def get_zones(sample_df):
    if sample_df['State'].isin(zone8_state):
        return 'zone_8'
    elif sample_df['State'].isin(zone7_state):
        return 'zone_7'
    elif sample_df['State'].isin(zone6_state):
        return 'zone_6'
    elif sample_df['State'].isin(zone5_state):
        return 'zone_5'
    elif sample_df['State'].isin(zone4_state) | sample_df['DISTRICT NAME'].isin(norcal):
        return 'zone_4'
    elif sample_df['DISTRICT NAME'].isin(zone3_city) | sample_df['Shipping City'].isin(zone3_city):
        return 'zone_3'
    elif sample_df['DISTRICT NAME'].isin(socal):
        return 'zone_2'
    else:
        return 'check the record'

THen I applied the function to my df:

sample_df['zone'] = sample_df.apply(get_zones, axis=1)

after I apply though, I get this:

AttributeError: 'str' object has no attribute 'isin'

can you please help me to get my function to work correctly? I also tried using 'contains' instead of 'isin' but also get a similar Attribute error. Thanks.

1 Answer

Think of it like this: when you call .apply with axis=1, the DataFrame gets passed to your get_zones function on a row-by-row basis, and the function will operate on each row individually. So within the get_zones() function, sample_df["State"] (or any other column you index) is a single (str) value corresponding to a specific row's entry for that column. Given this, you could modify your function to use Python's built-in in keyword, since you're just doing a simple membership check. In other words, instead of if sample_df['State'].isin(zone8_state):, try if sample_df['State'] in zone8_state: and so on.

Alternatively, you could abandon .apply and restructure get_zones() to return a list or array of zones corresponding to the rows of the input DataFrame, then just do sample_df["zone"] = get_zones(sample_df).

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Sophia Al-Mansoor

Sophia Al-Mansoor

Global Business & E-Commerce Reporter

Sophia analyzes international trade, startup ecosystems, retail transformation, and supply chain logistics for modern digital publications.

Share this article
Twitter Facebook Pinterest