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.
How do i fix this : AttributeError: 'str' object has no attribute 'current'
AttributeError: 'str' object has no attribute 'contains'
'Str' object has no attribute error?
python - Contains function in pandas dataframe column (AttributeError: 'str' object has no attribute 'str' - Stack Overflow
Videos
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()
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!
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.