I have a function that is supposed to take input, calculate the average and total as well as record count. The bug in the code is that: Even though I have added a try and except to catch errors, these errors are also being added to the count. How do I only count the integer inputs without making the “Invalid
Tag: conditional-statements
How to make an IF statement with conditions articulated with OR that stops as soon as the first True condition is reached?
Let’s take an example : I would like to check if the variable s is a string with length equal or less than 3. I tried the following : But it is not correct as the code considers the second condition whatever the result of the first one. For example, with s = 2, the code returns the error :
Is there a more elegant way of counting combinations of booleans from 2 arrays?
I tried to word the question as simply as possible but I’m new to Python and very bad at logic so I’m having a bit of trouble. Basically I want to know if there’s a cleaner way to count confusion matrices of two 1D arrays of booleans. Here’s an example: I tried this but it just adds more lines and
looping through dictionary and printing first value where condition is met
Having some trouble getting this simple conditional to work. I have a dictionary with 10 key-value pairs as follows: I’m trying to loop through the dictionary and print the key for the first value that is below 0.05. This is the code I have right now: Can anyone help me see where I’m going wrong here? Edit: It should be
How to use a loop to check multiple conditions on multiple columns to filter a dataframe in Python
I have a list containing names of the columns of a dataframe. The values for these columns are either ‘Yes’ or ‘No’. I want to filter out rows that’d have any of the columns having ‘Yes’. I want to use maybe a for loop to iterate through the list because the list is created from user input. So instead of
How to check if one column maps to specific value and vice verse?
I have a data frame with the following columns: First, I want to check that all 32835 values in the “zip_code” column match to a “part_no” with the following pattern, 01xxxxxx, where the Xs are numbers. Then, I want to make sure all 01xxxxxx part_no correspond to a 32835 “zip_code.” If not, I would like to return a list of
Add a comma after two words in pandas
I have the following texts in a df column: What I need is to add a comma at the end of each row but the condition that it is only two words. I found a partial solution: (solution found in stackoverflow) Answer Given: Doing: Outputs:
Merge inside the merge only if the first doesn’t return a match
I have 3 dataframes (df1, df2 & df3), the main one (df1) and two additional ones which contain 1 column amongst others that I want to bring over to the main dataframe. Sample dfs: df1 df2 and df3 I am using the following code: Then for the two empty strings for the “Objective” column I want to continue with a
Printing two values of one variable under different conditions
Please consider the statements below: sum_value = fixed_value – current_value, where fixed_value is a constant, and current_value is a function of thresholds; thresholds has two threshold_level values: thresholds = [10, 20]; I need to find a rato of sim_value corresponding to threshold_level = 10 to sim_value corresponding to threshold_level = 20, that is final_sim_value = sim_value_at_10/sim_value_at_20. The code part is
For loops and conditionals in Python
I am new to Python and I was wondering if there was a way I could shorten/optimise the below loops: I tried this oneliner, but it doesn’t seem to work: Answer You can use itertools.product to handle the nested loops for you, and I think (although I’m not sure because I can’t see your data) you can skip all the