Ok, at the risk of being ridiculed for not ‘trying harder’, I have a scenario that I’ve been attempting to adapt to a pythonic switch case statement. I know python has the new match method in 3.10 but I’m limited to 3.8.10 in my AWS use case. I’ve been reading up on switch cases in other languages and I want
Tag: conditional-statements
How can I find the sum of a users input?
Above is a code I have so far. It prints: My goal is for the code to add up the two services and make a total price. My end goal should look like this: Notice how the code added both prices and made a “Total.” Is there any way I can do this? I’m a beginner computer science major, so
Python – invalid syntax in conditions
I am studying how to use conditions in Python and I have no idea why on earth there’s a syntax error in my code, when I have just written normal condition: else y>200: Here is my code: Answer The should be You can look into the documentation for if statements: if Statements
Get value From Previous or Next Rows Based on Condition from two or more columns in Python
I am using Pandas Python to calculate timedelta between rows which when it uses previous or next rows is based on conditions. My table is like this sampel table I want to create timedelta column with this condition: it gets value from previous rows when STATUS > status_before and gets value from next rows when STATUS < status_after. I tried
Pandas fillna based on a condition
I’m still new to pandas, but I have a dataframe in the following format: and I’m trying to fill all NaN fields in the ‘d_header’ column using the following conditions: ‘d_header’ column should be set only for rows belonging to the same group the group should be determined by the ‘d_prefix’ column value of a row immediately after non-Nan ‘d_header’
Indent-Expected errors while trying to write one-line list comprehension: if-else variants
I am trying to write a list with if-else statements in one line. I’ve followed the instructions in this solution, but I got multiple “indent-Expected” errors. Here is my code: The issue in this line: Answer continue is a reserved keyword in Python so you are getting the error. You don’t need a continue to skip element. Further an if
Python : Amount input with While
Beginner here. I’m trying to build a loop where the chekout displays a total amount that has to be over 0$. For example, if I start with 450$ the code works. But if I start with say -12 it will ask me again (which is what I want), but then if I enter 450 as the third essay; the first
Transpose 3 column excel with K:V into column Pandas
I have a 3 column excel file I’m reading into pandas with basically k:v pairs in columns I need to not only tie the information in unnamed:1 & unnamed:2 to the unique animal ID as this is how I will track the animal but also transpose these columns where everything to the left of the “:” is the column header
Fastest way to use if/else statements when looping through dataframe with pandas
I am trying to run conditional statements when iterating through pandas df rows and it results with a very slow code. For example: The df is only about 40k rows long and it’s very slow, as this is only one of the statements I am trying to incorporate with this loop. Can you help with a faster way to do
Is there any possibility to create a new column based on the keywords list
Is there any possibility to create a new column based on the keywords list? I have data like this: I would like to create a new column if the keyword exists in the Type column and if 2 keywords exist then the value should both keyword with a comma. I am having a problem because I have to check also