close

[Solved] Attribute: ‘str’ object has no attribute

Hello Guys, How are you all? Hope You all Are Fine. Today I get the following error Attribute: ‘str’ object has no attribute in python. So Here I am Explain to you all the possible solutions here.

Without wasting your time, Let’s start This Article to Solve This Error.

How Attribute: ‘str’ object has no attribute Error Occurs?

Today I get the following error Attribute: ‘str’ object has no attribute in python.

How To Solve Attribute: ‘str’ object has no attribute Error ?

  1. How To Solve Attribute: 'str' object has no attribute Error ?

    To Solve Attribute: 'str' object has no attribute Error It means that the in operator is searching your empty string in the index, not the contents of it.

  2. Attribute: 'str' object has no attribute

    To Solve Attribute: 'str' object has no attribute Error It means that the in operator is searching your empty string in the index, not the contents of it.

Solution 1


df['a']
 returns a Series object that has astype as a vectorized way to convert all elements in the series into another one.

df['a'][1] returns the content of one cell of the dataframe, in this case the string '0.123'. This is now returning a str object that doesn’t have this function. To convert it use regular python instruction:

type(df['a'][1])
Out[25]: str

float(df['a'][1])
Out[26]: 0.123

type(float(df['a'][1]))
Out[27]: float

As per your second question, the operator in that is at the end calling __contains__ against the series with '' as argument, here is the docstring of the operator:

help(pd.Series.__contains__)
Help on function __contains__ in module pandas.core.generic:

__contains__(self, key)
    True if the key is in the info axis

It means that the in operator is searching your empty string in the index, not the contents of it.

The way to search your empty strings is to use the equal operator:

df
Out[54]: 
    a
0  42
1    

'' in df
Out[55]: False

df==''
Out[56]: 
       a
0  False
1   True

df[df['a']=='']
Out[57]: 
  a
1 

Solution 2


df['a'][1]
 will return the actual value inside the array, at the position 1, which is in fact a string. You can convert it by using float(df['a'][1]).

>>> df = pd.DataFrame({'a':['1.23', '0.123']})
>>> type(df['a'])
<class 'pandas.core.series.Series'>
>>> df['a'].astype(float)
0    1.230
1    0.123
Name: a, dtype: float64
>>> type(df['a'][1])
<type 'str'>

For the second question, maybe you have an empty value on your raw data. The correct test would be:

>>> df = pd.DataFrame({'a':['1', '']})
>>> '' in df['a'].values
True

Summery

It’s all About this issue. Hope all solution helped you a lot. Comment below Your thoughts and your queries. Also, Comment below which solution worked for you? Thank You.

Also, Read