I think you are looking for in:
if 'goat' in 'goat cheese':
print('beeeeeeh!')
Answer from gosuto on Stack OverflowI think you are looking for in:
if 'goat' in 'goat cheese':
print('beeeeeeh!')
You might be confusing .str.contains() from pandas, which exists and is applied to series. In this case you can use in or not in operators.
Here's a full guide on how to address the issue Does Python have a string 'contains' substring method?
From pandas docs:
Series.str.contains(self, pat, case=True, flags=0, na=nan, regex=True). Test if pattern or regex is contained within a string of a Series or Index.
row['name'] is a dictionary with the value being the string you are searching. To search in a vectorized way, you don't need to use iterrows():
df['name'].str.contains('abc') will return a boolean index because it uses the pandas Series string contains method.
At the row level (if this is what you want) just use in:
for idx, row in df.iterrows():
if 'abc' in row['name']:
row['Name'] will return a string not a pandas series so you can't use .str.contains Instead of looping row by row you can apply it to the whole column df['row'].str.contains('abc') This will return a series of type bool.
python - Use str.contains in pandas with apply statement raises str object has not attribute str error - Stack Overflow
using csv instead of geodataframe: 'str' object has no attribute 'contains'
python - AttributeError: 'str' object has no attribute 'contains' - Stack Overflow
I get the error message AttributeError: 'DataFrame' object has no attribute 'str on str.split method
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")The error message states that you're trying to use .contains() on a string which is not a string function.
Try:
for i in range(0,len(final3)):
default = final3["DefaultValue"].iloc[i]
if not "|" in default:
if default in final3["CodedData"].iloc[i]:
final3["Condition"].iloc[i] = "False"
else:
final3["Condition"].iloc[i] = "True"
In python strings, there is no method contains. Instead python has a simple syntax for that. You can use the following in place of default.contains("|")
"|" in default
#or
"|" not in default
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?
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
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)