Discussions

python - Use str.contains in pandas with apply statement raises str object has not attribute str error - Stack Overflow
I thought it would be simply, but I'm stumbling on this for a while now. I have a column containing several information but depending on some content I'd like to label ist with a category: import More on stackoverflow.com
🌐 stackoverflow.com
using csv instead of geodataframe: 'str' object has no attribute 'contains'
Aye! I love GeoPandas :) so... there is a lot to unpack here, but I'll just answer your question and let you decide whether it works for you or not (i.e. why do you need csv? you can keep the integrity of your geo file in-tact with gdf.to_file() -> creates a shapefile instead). You need to convert your csv back into the proper data type. from shapely import wkt gdf.geometry = gdf.geometry.apply(wkt.loads) However FYI this means you're essentially reading your GDF, converting your GDF into CSV, then converting your CSV back to GDF. Hope this helps! Edit* Technically speaking, the CSV is readable by geopandas, its only the geometry column that gets messed up, and hence the only one you need to fix. More on reddit.com
🌐 r/learnpython
5
0
December 6, 2021
python - AttributeError: 'str' object has no attribute 'contains' - Stack Overflow
"DefaultValue" column contains values like "1|2|3". So, I tried to split the string using delimiter and check if it exist in the "CodedData" column in the same row. "CodedData" contains values like "3|4" But I am facing the attribute error. More on stackoverflow.com
🌐 stackoverflow.com
I get the error message AttributeError: 'DataFrame' object has no attribute 'str on str.split method
Have you tried setting column values to string Df["column name"].to_string() Or Are you splitting. df instead of df.values More on reddit.com
🌐 r/learnpython
2
3
March 21, 2021
🌐
Reddit
reddit.com › r/learnpython › how can i use pandas .values with str.contains/lower?
r/learnpython on Reddit: How can I use pandas .values with str.contains/lower?
June 15, 2021 -

Hello! I could use some help trying to figure out how to find a string within a column in my dataframe and convert it to a lower case since some words may be upper or lower. My end goal is to use an IF condition based on what is stored in the dataframe. I'm sure there might be a better way then what I am trying to do but any help would be greatly appreciated, thanks.

Error:

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

Current Code:

global dfname
dfname = mergedf.values[0][1]

global dfman

dfman = mergedf.values[0][3]

global dfstatus

dfstatus = mergedf.values[0][4]

global dfurl

dfurl = mergedf.values[0][5]

global dfstatusboolean

dfstatusboolean = mergedf.values[0][6]

a = dfname.str.lower().str.contains("value a")
b = dfman.str.lower().str.contains("value b")
c = dfstatus.str.lower().str.contains("value c")
🌐
Researchdatapod
researchdatapod.com › home › how to solve python attributeerror: ‘str’ object has no attribute ‘contains’
How to Solve Python AttributeError: 'str' object has no attribute 'contains' - The Research Scientist Pod
March 30, 2022 - To solve this error, you can use the in operator to check for membership in a string. Or call the str.contains() method on the Series instead of a string value in the Series. It is helpful to print the type of the object before calling the contains ...
🌐
Brainly
brainly.com › engineering › college › what does "str object has no attribute" mean in python?
[FREE] What does "str object has no attribute" mean in Python? - brainly.com
The second search result explains that the error message specifically tells you that there is no attribute named 'append' in the 'str' object. The third search result discusses how the str attribute belongs to the pandas.
🌐
tutorialpedia
tutorialpedia.org › blog › str-contains-pandas-returns-str-object-has-no-attribute-contains
How to Fix 'str' Object Has No Attribute 'contains' in Pandas: Search List of Words in a String (Tweet Analysis Guide) — tutorialpedia.org
The error 'str' object has no attribute 'contains' arises when you mistakenly call contains() directly on a single string (e.g., df['tweet'][0].contains("climate")) instead of using the str accessor on the Series (e.g., df['tweet'].str.cont...
🌐
Reddit
reddit.com › r/learnpython › using csv instead of geodataframe: 'str' object has no attribute 'contains'
using csv instead of geodataframe: 'str' object has no attribute 'contains' : r/learnpython
December 6, 2021 - Nvm. I had changed the if gdf['geometry'][i].contains(point) == True part to if point in gdf['geometry'][i] to see if that worked, and forgot to change it back. Now I changed it back, and it seems to work!
Find elsewhere
🌐
Google Groups
groups.google.com › g › pydata › c › FBLwPdA44cM
How to use str.contains on a DataFrame's index?
May 8, 2013 - The method above doesn't work: In [2]: ex.index.str.contains('Dec') AttributeError: 'Index' object has no attribute 'str' I can write a little function and use map() (which is what I actually did), but it would be nicer if there were some easy way to use all of pandas' vectorized functions ...
🌐
Data Science Learner
datasciencelearner.com › home › ‘str’ object has no attribute ‘contains’ ( solved )
'str' object has no attribute 'contains' ( Solved )
April 14, 2023 - my_string = "Welcome to the Data ... ‘contains’ error if you use the” in ” operator instead of the contains() method for checking the existence of a substring in a string....
🌐
Saturn Cloud
saturncloud.io › blog › how-to-solve-the-str-object-has-no-attribute-contains-error-in-pandas
How to Solve the str Object has no Attribute contains Error in Pandas | Saturn Cloud Blog
October 4, 2023 - To fix the 'str' object has no attribute 'contains' error, you need to ensure that you are applying the str.contains() method to a Pandas Series or DataFrame column or row that contains string values.
🌐
Easy Tweaks
easytweaks.com › attributeerror-str-object-attribute-contains
How to fix the attributeerror 'str' object has no attribute 'contains'?
October 30, 2022 - Skip to content · BECOMING MORE EFFICIENT WITH TECH · HELPING TO REDUCE BUSY WORK SO YOU CAN FOCUS ON STUFF THAT MATTERS · Check out our latest posts: · How to change the default Email Account in Gmail and Outlook · How to fix Windows 11 Not detecting Bluetooth headset · How to fix Microsoft ...
🌐
Reddit
reddit.com › r/learnpython › i get the error message attributeerror: 'dataframe' object has no attribute 'str on str.split method
r/learnpython on Reddit: I get the error message AttributeError: 'DataFrame' object has no attribute 'str on str.split method
March 21, 2021 -

Hi, I'm trying to run a str.split method on a simple Pandas dataframe that has a ID column and text column that is of 'object' type and get the message AttributeError: 'DataFrame' object has no attribute 'str' . I think the column should be in a series format to run the str.split method and have tried to change the datatype to 'str', but the datatype stays a an object. How can I get the column into a series object to run a series method?

🌐
Itsourcecode
itsourcecode.com › home › attributeerror: ‘dataframe’ object has no attribute ‘str’ [solved]
attributeerror: 'dataframe' object has no attribute 'str' [Solved]
April 5, 2023 - The first step in fixing the AttributeError: ‘DataFrame’ object has no attribute ‘str’ error is to check the data types of the DataFrame columns. If a column does not contain string values, it cannot be manipulated with string methods.
Top answer
1 of 2
28

Short answer: change data.columns=[headerName] into data.columns=headerName

Explanation: when you set data.columns=[headerName], the columns are MultiIndex object. Therefore, your log_df['Product'] is a DataFrame and for DataFrame, there is no str attribute.

When you set data.columns=headerName, your log_df['Product'] is a single column and you can use str attribute.

For any reason, if you need to keep your data as MultiIndex object, there is another solution: first convert your log_df['Product'] into Series. After that, str attribute is available.

products = pd.Series(df.Product.values.flatten())
include_clique = products[products.str.contains("Product A")]

However, I guess the first solution is what you're looking for

2 of 2
2

You get AttributeError: 'DataFrame' object has no attribute ... when you try to access an attribute your dataframe doesn't have.

A common case is when you try to select a column using . instead of [] when the column name contains white space (e.g. 'col1 ').

df.col1       # <--- error
df['col1 ']   # <--- no error

Another common case is when you try to call a Series method on a DataFrame. For example, tolist() (or map()) are Series methods so they must be called on a column. If you call them on a DataFrame, you'll get

AttributeError: 'DataFrame' object has no attribute 'tolist'

AttributeError: 'DataFrame' object has no attribute 'map'

As hoang tran explains, this is what is happening with OP as well. .str is a Series accessor and it's not implemented for DataFrames.


Yet another case is if you have a typo and try to call/access an attribute that's simply not defined; e.g. if you try to call rows() instead of iterrows(), you'll get

AttributeError: 'DataFrame' object has no attribute 'rows'

You can check the full list of attributes using the following comprehension.

[x for x in dir(pd.DataFrame) if not x.startswith('_')]

When you assign column names as df.columns = [['col1', 'col2']], df is a MultiIndex dataframe now, so to access each column, you'll need to pass a tuple:

df['col1'].str.contains('Product A')    # <---- error
df['col1',].str.contains('Product A')   # <---- no error; note the trailing comma

In fact, you can pass a tuple to select a column of any MultiIndex dataframe, e.g.

df['level_1_colname', 'level_2_colname'].str.contains('Product A')

You can also flatten a MultiIndex column names by mapping a "flattener" function on it. A common one is ''.join:

df.columns = df.columns.map('_'.join)
🌐
Researchdatapod
researchdatapod.com › home › how to solve pandas attributeerror: ‘dataframe’ object has no attribute ‘str’
How to Solve Pandas AttributeError: 'DataFrame' object has no attribute 'str' - The Research Scientist Pod
May 14, 2022 - --------------------------------------------------------------------------- AttributeError Traceback (most recent call last) Input In [10], in <cell line: 1>() ----> 1 veg_pizza = df.loc[df['pizza'].str.contains('veg')] File ~/opt/anaconda3/lib/python3.8/site-packages/pandas/core/generic.py:5583, in NDFrame.__getattr__(self, name) 5576 if ( 5577 name not in self._internal_names_set 5578 and name not in self._metadata 5579 and name not in self._accessors 5580 and self._info_axis._can_hold_identifiers_and_holds_name(name) 5581 ): 5582 return self[name] -> 5583 return object.__getattribute__(self, name) AttributeError: 'DataFrame' object has no attribute 'str'
🌐
Bobby Hadz
bobbyhadz.com › blog › python-attributeerror-str-object-has-no-attribute
AttributeError: 'str' object has no attribute 'X in Python | bobbyhadz
April 8, 2024 - If you try to access any attribute that is not in this list, you will get the "AttributeError: str object has no attribute error". Since contains() is not a method implemented by strings, the error is caused.