conditional

Creating a new column based on if-elif-else condition

人走茶凉 提交于 2019-12-27 11:54:09
问题 I have a DataFrame df : A B a 2 2 b 3 1 c 1 3 I want to create a new column based on the following criteria: if row A == B: 0 if row A > B: 1 if row A < B: -1 so given the above table, it should be: A B C a 2 2 0 b 3 1 1 c 1 3 -1 For typical if else cases I do np.where(df.A > df.B, 1, -1) , does pandas provide a special syntax for solving my problem with one step (without the necessity of creating 3 new columns and then combining the result)? 回答1: To formalize some of the approaches laid out

How to create a new variable with values from different variables if another variable equals a set value in R?

天涯浪子 提交于 2019-12-25 18:58:22
问题 I have a complicated question that I will try to simplify by simplifying my dataset. Say I have 5 variables: df$Id <- c(1:12) df$Date <- c(NA,NA,a,a,b,NA,NA,b,c,c,b,a) df$va <- c(1.1, 1.4, 2.5, ...) #12 randoms values df$vb <- c(5.9, 2.3, 4.7, ...) #12 other random values df$vc <- c(3.0, 3.3, 3.7, ...) #12 more random values Then I want to create a new variable that takes the value from va, vb, or vc if the date is equal to a, b, or c. I had tried a nested if-else, which did not work. I also

Combining multiple conditional expressions in a list comprehension

十年热恋 提交于 2019-12-25 18:24:30
问题 I utf-8 encode characters like \u2013 before inserting them into SQLite. When I pull them out with a SELECT, they are back in their unencoded form, so I need to re-encode them if I want to do anything with them. In this case, I want to write the rows to a CSV.Before writing the rows to CSV, I want to first add hyperlink to any row whose value starts with 'http'. Some values will be ints, dates etc, so I do the folliowing conditional expression - list comprehension combo: row = ['=HYPERLINK("

Error when subsetting based on adjusted values of different data frame in R

对着背影说爱祢 提交于 2019-12-25 17:08:32
问题 I am asking a side-question about the method I learned here from @redmode : Subsetting based on values of a different data frame in R When I try to dynamically adjust the level I want to subset by: N <- nrow(A) cond <- sapply(3:N, function(i) sum(A[i,] > 0.95*B[i,])==2) rbind(A[1:2,], subset(A[3:N,], cond)) I get an error Error in FUN(left, right) : non-numeric argument to binary operator. Can you think of a way I can get rows pertaining to values in A that are greater than 95% of the value

Mysql CASE - WHEN - THEN - returning wrong data type (blob)

≡放荡痞女 提交于 2019-12-25 08:58:02
问题 Im creating customizable product attributes in a web store - each attribute can have different type of data, each data type is stored in a separate column using corresponding mysql datatype. I Have a query like: SELECT products.id AS id, products.sku AS sku, products.name AS name, products.url_key AS url_key, attributes.name AS attribute, CASE WHEN `attribute_types`.`type` = 'text' THEN product_attribute_values.value_text WHEN `attribute_types`.`type` = 'float' THEN product_attribute_values

Angular ng-options with conditional on value in simple array of strings

僤鯓⒐⒋嵵緔 提交于 2019-12-25 08:14:48
问题 I have two array shaped like this: $scope.values_array = ['string1', 'string2', 'string3', 'string4']; $scope.skip_array = ['string2', 'string3']; and I try to use ng-options like this: <select ng-options="value as value for value in values_array && skip_array.indexOf(value) === -1"></select> This doesn't produce an error but nothing shows up in the dropdown anymore. Without the "&& skip_array.indexOf(value)" it shows all the options in values_array. 回答1: html: <select ng-model="selectedValue

Excel - Conditional Formatting. Referring to a styled cell instead of having fixed formatting

☆樱花仙子☆ 提交于 2019-12-25 05:18:08
问题 I believe I'm asking the same question as in this thread but it wasn't answered. Here's my problem. So, applied to the range K3:K10 , I have several different conditional formatting rules that require a specific text string comprised of the contents of a referenced cell plus a constant text string. Each rule applies a different colour for a specific referenced cell and constant text string (DEAD or ALIVE) combination. For example, in the range, if a cell contains the word "Dog", then a space,

Google apps script - using for loop to pull data from range based on condition

那年仲夏 提交于 2019-12-25 04:07:33
问题 I'm having an issue pulling the correct values out of a for loop in Google Sheets. Here's my code: Note: this is a snippet from a larger function function sendEmails() { var trackOriginSheet = SpreadsheetApp.getActiveSpreadsheet().getName(); var getMirSheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Miranda"); //Set a new object to hold conditional data var holdingData = new Object(); //Create function to get values from origin sheet var returnedValues = function

Value error: truth value ambiguous

懵懂的女人 提交于 2019-12-25 03:49:13
问题 Following up from here: Conditional calculation in python I'm editing this line: out = log(sum(exp(a - a_max), axis=0)) from line 85 here: https://github.com/scipy/scipy/blob/v0.14.0/scipy/misc/common.py#L18 to this: out = log(sum(exp(threshold if a - a_max < threshold else a - a_max), axis = 0)) But I'm getting the following error: out = log(sum(exp(threshold if a - a_max < threshold else a - a_max), axis=0)) ValueError: The truth value of an array with more than one element is ambiguous.

MySQL: is AND conditional or logical? [duplicate]

岁酱吖の 提交于 2019-12-25 02:16:29
问题 This question already has answers here : MySQL - AND condition (5 answers) Closed 5 years ago . If I run such a query in MySQL: SELECT field FROM table WHERE field1=42 AND field2 LIKE '%beer%'; Will every row be parsed for having "beer" in field2 or only those whose field1 =42? 回答1: That depends on the query plan and what indexes you have on the table. Try putting EXPLAIN before your query and MySQL will tell you more. Generally speaking though, if field1 is indexed, then LIKE should only be