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.
If obj.contains(mouse): AttributeError: 'str' object has no attribute 'contains'
AttributeError: 'str' object has no attribute 'contains'
python - Contains function in pandas dataframe column (AttributeError: 'str' object has no attribute 'str' - Stack Overflow
python - how to use contains when: AttributeError: 'str' object has no attribute 'contains' - Stack Overflow
Videos
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.
You could avoid using for loop altogether.
Why not just use df["name"].str.contains("Ac|Vt")?
You could add the result as a separate column too:
df.loc[df["name"].str.contains("Ac|Vt"), "status"] = "good"
Let's try using DataFrame.iterrows,
for _, j in df.iterrows():
if "Ac" in j['name'] or "Vt" in j['name']:
print('Good')
There is something wrong with this function. It shows no errors, but when I try to run it, It says AttributeError: 'str' object has no attribute 'current'. (BTW, i am trying to run it as a flet on spyder)
def leapyears(e):
days_in_month = {1: 31, 3: 31, 4: 30, 5:31, 6: 30, 7: 31, 8: 31, 9: 30, 10: 31, 11: 30, 12: 31 }
month = int(EnterMonth_text.value)
year = int(EnterYear_text.value)
if year % 100 == 0:
if year % 400 == 0:
leap_year = True
elif year % 4 == 0:
leap_year = True
else:
leap_year = False
if month == 2 :
if leap_year:
days_in_month[2] = 29
else:
days_in_month[2]= 28
output_textfield.value= days_in_month[month]
page.update()
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
I have some experience with programming in Java, C++, etc. and I am trying to write a simple "To-Do List" program to get used to Python. I'm running into the error: str object has no attribute "completed" when trying to iterate over the list of tasks, check their completion status, and display them.
Here are some relevant pieces of the program:
Constructor for the Task class
def __init__(self, task_name):
self.task_name = task_name
self.completed = False
In the ToDoList class (which holds a list of the task instances created by the user) this is the iteration throwing the error in question:
for idx, task in enumerate(self.tasks, start=1):
status = "Completed" if task.completed else "Incomplete"
print(f"{idx}. {task.task_name} - {status}")
I thought, potentially the problem lies in the fact that the enumerate function is grabbing the string value of the task instance, rather than the object itself, so maybe I can iterate over it the old fashioned way and get around it. So I tried it like this:
counter = 1
for task in self.tasks:
status = "Completed" if task.completed else "Incomplete"
print(f"{counter}. {task.task_name} - {status}")
counter += 1
Yet, it throws the same error. I know there is something I am missing or not understanding correctly here. What is it?
Thanks!