You can handle the positive case using the following:
In [150]:
import re
df['fundleverage'] = '+' + df['name'].str.extract(r"(X\d+|\d+X)\s", flags=re.IGNORECASE).str.strip('X') + '00'
df
Out[150]:
name fundleverage
0 BULL AXP UN X3 VON +300
1 BULL ESTOX X12 S +1200
You can use np.where to handle both cases in a one liner:
In [151]:
df['fundleverage'] = np.where(df['name'].str.extract(r"(X\d+|\d+X)\s", flags=re.IGNORECASE).str.strip('X').str.isdigit(), '+' + df['name'].str.extract(r"(X\d+|\d+X)\s", flags=re.IGNORECASE).str.strip('X') + '00', '+100')
df
Out[151]:
name fundleverage
0 BULL AXP UN X3 VON +300
1 BULL ESTOX X12 S +1200
So the above uses the vectorised str methods strip, extract and isdigit to achieve what you want.
Update
After you changed your requirements (which you should not do for future reference) you can mask the df for the bull and bear cases:
In [189]:
import re
df = pd.DataFrame(["BULL AXP UN X3 VON", "BEAR ESTOX 12x S"], columns=["name"])
bull_mask_name = df.loc[df['name'].str.contains('bull', case=False), 'name']
bear_mask_name = df.loc[df['name'].str.contains('bear', case=False), 'name']
df.loc[df['name'].str.contains('bull', case=False), 'fundleverage'] = np.where(bull_mask_name.str.extract(r"(X\d+|\d+X)\s", flags=re.IGNORECASE).str.strip('X').str.isdigit(), '+' + bull_mask_name.str.extract(r"(X\d+|\d+X)\s", flags=re.IGNORECASE).str.strip('X') + '00', '+100')
df.loc[df['name'].str.contains('bear', case=False), 'fundleverage'] = np.where(bear_mask_name.str.extract(r"(X\d+|\d+X)\s", flags=re.IGNORECASE).str.strip('x').str.isdigit(), '-' + bear_mask_name.str.extract(r"(X\d+|\d+X)\s", flags=re.IGNORECASE).str.strip('x') + '00', '-100')
df
Out[189]:
name fundleverage
0 BULL AXP UN X3 VON +300
1 BEAR ESTOX 12x S -1200
Answer from EdChum on Stack OverflowAttributeError: 'str' object has no attribute 'get'
'Str' object has no attribute error?
Result: Failure Exception: AttributeError: 'str' object has no attribute 'get' at project_id = req_body.get("project_id", None) this
AttributeError: 'str' object has no attribute 'get'
Videos
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!
values = [0] data = [] ROWS = 3 COLS = 2
for r in range(ROWS):
for c in range(COLS): values = (input(f'Enter a value for row {r} column {c}: ')) data.append(values) values.append(data)
print(values)
At this line:zipfile.ZipFile.extractall(zip, None, pwd=str.encode(pwd))
i allways get the error (in the title).
My zip path (as string zip) is like this:zipFile = r'/home/itsthooor/This That/Empty.zip'
Where's the problem?
I saw a stack overflow post about it, but it didn't me help at all.
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()