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

Answer from hoang tran on Stack Overflow
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)
🌐
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?

Discussions

python - pandas - 'dataframe' object has no attribute 'str' - Stack Overflow
I am trying to filter out the dataframe that contains a list of product. However, I am getting the pandas - 'dataframe' object has no attribute 'str' error whenever I run the code. Here is the li... More on stackoverflow.com
🌐 stackoverflow.com
July 24, 2018
pandas - Python AttributeError: 'str' object has no attribute 'DataFrame' - Stack Overflow
import pandas as pd ProdDate = ... df = pd.DataFrame(ProdDate, columns = ['Date']) works fine. which is why this is confusing because now date is a list of 250000 values which has been working no problem until I added a few lines of code above and now this line returns · AttributeError: 'str' object has no attribute ... More on stackoverflow.com
🌐 stackoverflow.com
python - 'DataFrame' object has no attribute 'str' ​ - Stack Overflow
What is sa[col]? The error is telling you that str is not a member of sa[col]. That's a good place to start looking at what might be going on ... Sorry, sa is my actual dataframe, I was keeping it 'df' for the purpose of alias. It's sa.loc[sa[col] in my notebook anyway. ... This could be because your dataframe has ... More on stackoverflow.com
🌐 stackoverflow.com
AttributeError: 'DataFrame' object has no attribute 'name'; Various stack overflow / github suggested fixes not working
AttributeError: 'DataFrame' object has no attribute 'name'; Various stack overflow / github suggested fixes not working#8624 ... I perform a pipeline of transformations on a Dask dataframe originating from dd.read_sql_table() from a view in an oracle DB. In one stage that follows many successful stages, I try to apply a simple class method to a column of type (strings... More on github.com
🌐 github.com
10
January 26, 2022
🌐
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.
🌐
GitHub
github.com › dask › dask › issues › 8624
AttributeError: 'DataFrame' object has no attribute 'name'; Various stack overflow / github suggested fixes not working · Issue #8624 · dask/dask
January 26, 2022 - # Create a pandas DataFrame having the information needed to look up # the state_id, city_id, district_id, given the name of a district: self.pd_df_of_ids = self.pd.read_csv('distinct_cities_to_city_and_state_ids.csv') # self.pd_df_of_id looks like this: # district state_id city_id district_id # <object> <numpy.int8> <numpy.int64> <numpy.int64> # "Tempe, AZ, Unicersity" 2 8675309 5551212 # 'Santa Monica, CA N, Ocean ' 5 1234 5678 # ... # Define a method that takes a district as a string an returns the Dask series def find_city_and_state(self,district_str) query = "district == '{district_str}'" row = self.pd_df_of_ids.query(query) if row.shape[0] == 1: locale_info_pd_Series =\ pd.series([ row['state_id'].values[0], row['city_id'].values[0], row['district_id'].values[0] ], index=[ 'state_id', 'city_id', 'district_id']) else: locale_info_pd_series =\ pd.Series([ 0.
Author   david-thrower
Find elsewhere
Top answer
1 of 5
2

"sklearn.datasets" is a scikit package, where it contains a method load_iris().

load_iris(), by default return an object which holds data, target and other members in it. In order to get actual values you have to read the data and target content itself.

Whereas 'iris.csv', holds feature and target together.

FYI: If you set return_X_y as True in load_iris(), then you will directly get features and target.

from sklearn import datasets
data,target = datasets.load_iris(return_X_y=True)
2 of 5
1

The Iris Dataset from Sklearn is in Sklearn's Bunch format:

print(type(iris))
print(iris.keys())

output:

<class 'sklearn.utils.Bunch'>
dict_keys(['data', 'target', 'target_names', 'DESCR', 'feature_names', 'filename'])

So, that's why you can access it as:

x=iris.data
y=iris.target

But when you read the CSV file as DataFrame as mentioned by you:

iris = pd.read_csv('iris.csv',header=None).iloc[:,2:4]
iris.head()

output is:

    2   3
0   petal_length    petal_width
1   1.4 0.2
2   1.4 0.2
3   1.3 0.2
4   1.5 0.2

Here the column names are '1' and '2'.

First of all you should read the CSV file as:

df = pd.read_csv('iris.csv')

you should not include header=None as your csv file includes the column names i.e. the headers.

So, now what you can do is something like this:

X = df.iloc[:, [2, 3]] # Will give you columns 2 and 3 i.e 'petal_length' and 'petal_width'
y = df.iloc[:, 4] # Label column i.e 'species'

or if you want to use the column names then:

X = df[['petal_length', 'petal_width']]
y = df.iloc['species']

Also, if you want to convert labels from string to numerical format use sklearn LabelEncoder

from sklearn import preprocessing
le = preprocessing.LabelEncoder()
y = le.fit_transform(y)
🌐
CSDN
devpress.csdn.net › python › 63045ca47e6682346619a86b.html
pandas - 'dataframe' object has no attribute 'str'_python_Mangs-Python
August 23, 2022 - 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.
🌐
Edureka Community
edureka.co › home › community › categories › python › python pandas error attributeerror dataframe ...
Python Pandas error AttributeError DataFrame object has no attribute rows | Edureka Community
March 28, 2019 - I am trying to print each entry of the dataframe separately. The dataframe is created by reading ... : 'DataFrame' object has no attribute 'rows'
🌐
GitHub
github.com › pandas-dev › pandas › issues › 23211
Expand .str to DataFrame · Issue #23211 · pandas-dev/pandas
October 17, 2018 - Code Sample, a copy-pastable example if possible mask = df.str.match(re) AttributeError: 'DataFrame' object has no attribute 'str' Problem description I always run into this. DataFr...
Author   sorenwacker
🌐
Fast.ai
forums.fast.ai › part 1 (2019)
AttributeError: 'str' object has no attribute 'paragraphs' - Part 1 (2019) - fast.ai Course Forums
December 2, 2020 - Hello. I am trying to create a language model from blogs. I am calling the Datasets API to build a language model from blogs. dsets = Datasets(df_p, [tfms],dl_type=LMDataLoader) df_p is a pandas dataframe that contains blogs. When we print: print(df_p.head()) Result is: 0 ['12 years ago, the smartphone was in its infancy.
Top answer
1 of 2
9

First problem shoud be duplicated columns names, so after select colB get not Series, but DataFrame:

df = pd.DataFrame([['Example: s', 'as', 2], ['dd', 'aaa', 3]], columns=['colB','colB','colC'])
print (df)
         colB colB  colC
0  Example: s   as     2
1          dd  aaa     3

print (df['colB'])
         colB colB
0  Example: s   as
1          dd  aaa

#print (df['colB'].str.contains('Example:'))
#>AttributeError: 'DataFrame' object has no attribute 'str'

Solution should be join columns together:

print (df['colB'].apply(' '.join, axis=1))
0    Example: s as
1           dd aaa

df['colB'] = df.pop('colB').apply(' '.join, axis=1)
df = df[~df['colB'].str.contains('Example:')] 
print (df)
   colC    colB
1     3  dd aaa

Second problem should be hidden MultiIndex:

df = pd.DataFrame([['Example: s', 'as', 2], ['dd', 'aaa', 3]], columns=['colA','colB','colC'])
df.columns = pd.MultiIndex.from_arrays([df.columns])
print (df)
         colA colB colC
0  Example: s   as    2
1          dd  aaa    3

print (df['colB'])
  colB
0   as
1  aaa

#print (df['colB'].str.contains('Example:'))
#>AttributeError: 'DataFrame' object has no attribute 'str'

Solution is reassign first level:

df.columns = df.columns.get_level_values(0)
df = df[~df['colB'].str.contains('Example:')] 
print (df)
         colA colB  colC
0  Example: s   as     2
1          dd  aaa     3

And third should be MultiIndex:

df = pd.DataFrame([['Example: s', 'as', 2], ['dd', 'aaa', 3]], columns=['colA','colB','colC'])
df.columns = pd.MultiIndex.from_product([df.columns, ['a']])
print (df)
         colA colB colC
            a    a    a
0  Example: s   as    2
1          dd  aaa    3

print (df['colB'])
     a
0   as
1  aaa

print (df.columns)
MultiIndex(levels=[['colA', 'colB', 'colC'], ['a']],
           codes=[[0, 1, 2], [0, 0, 0]])

#print (df['colB'].str.contains('Example:'))
#>AttributeError: 'DataFrame' object has no attribute 'str'

Solution is select MultiIndex by tuple:

df1 = df[~df[('colB', 'a')].str.contains('Example:')] 
print (df1)
         colA colB colC
            a    a    a
0  Example: s   as    2
1          dd  aaa    3

Or reassign back:

df.columns = df.columns.get_level_values(0)
df2 = df[~df['colB'].str.contains('Example:')] 
print (df2)
         colA colB  colC
0  Example: s   as     2
1          dd  aaa     3

Or remove second level:

df.columns = df.columns.droplevel(1)
df2 = df[~df['colB'].str.contains('Example:')] 
print (df2)
         colA colB  colC
0  Example: s   as     2
1          dd  aaa     3
2 of 2
0

Try this:

df[[~df.iloc[i,:].str.contains('String_to_match').any() for i in range(0,len(df))]]

🌐
Python Forum
python-forum.io › thread-32955.html
'str' object has no attribute 'to_csv'
Hello guys, I'm trying to save some data that I collected from a website textform, on a csv file. And for that I'm using the following code, but I'm getting the error: 'str' object has no attribute 'to_csv' import zapimoveis_scraper as zap import ...
🌐
Nextstrain
discussion.nextstrain.org › t › pandas-error-during-augur-filter-nonetype-object-has-no-attribute-str › 1420
Pandas error during augur filter: 'NoneType' object has no attribute 'str' - Nextstrain Discussion
August 10, 2023 - Hi, I get this error when running augur filter, and I’m not quite sure how to investigate the problem. I run this build every week with Gisaid data, so I suspect there might be some strange strain names or something? […
🌐
GitHub
github.com › zmcddn › Data-Science-Helper › issues › 1
'str' object has no attribute 'columns' · Issue #1 · zmcddn/Data-Science-Helper
January 16, 2019 - Traceback (most recent call last): ...hon3.6/site-packages/dshelper/dshelper.py", line 435, in prepare_df columns = list(df.columns.values) AttributeError: 'str' object has no attribute 'columns'...
Author   huangzhenhao90
🌐
GitHub
github.com › pycaret › pycaret › issues › 195
AttributeError: 'DataFrame' object has no attribute 'dtype' · Issue #195 · pycaret/pycaret
June 3, 2020 - This is necessary when loading ... -> 5274 return object.__getattribute__(self, name) 5275 5276 def __setattr__(self, name: str, value) -> None: AttributeError: 'DataFrame' object has no attribute 'dtype'...
Author   sorenwacker