Attributeerror: ‘str’ object has no attribute ‘isin’

AttributeError: ‘str’ object has no attribute ‘isin’

This error message in Python typically occurs when you try to use the isin function on a string object, but the isin function is not a valid method for strings.

The isin function is used to check whether elements in a specific series or DataFrame belong to a particular set of values. However, in this case, you are trying to use it on a string, which doesn’t have the isin method, resulting in an AttributeError.

Here’s an example to better understand the error:

    
      cities = "New York, London, Paris, Tokyo"
      print(cities.isin(["London", "Paris"]))
      # AttributeError: 'str' object has no attribute 'isin'
    
  

In the above example, we have a string called cities, which is not a Pandas Series or DataFrame. When we try to use the isin function on a string, it raises an AttributeError because the method doesn’t exist for strings.

Solution:

To resolve this error, you need to make sure you are using the isin function on a Pandas Series or DataFrame, not on a string object. Here’s an updated example:

    
        import pandas as pd

        cities = pd.Series(["New York", "London", "Paris", "Tokyo"])
        print(cities.isin(["London", "Paris"]))
        # Output: [False, True, True, False]
    
  

In the above example, we have created a Pandas Series called cities using pd.Series(). Now we can successfully use the isin function on the cities Series to check whether each element belongs to the specified set of values.

Read more interesting post

Leave a comment