conditional-statements

Adding new column with conditional values using ifelse

青春壹個敷衍的年華 提交于 2019-12-08 04:30:53
问题 I have a data frame with more than 400.000 observations and I'm trying to add a column to it which its values depend on another column and sometimes multiple ones. Here is a simpler example of what I'm trying to do : # Creating a data frame M <- data.frame(c("A","B","C"),c(5,100,60)) names(M) <- c("Letter","Number") #adding a column M$Size <- NA # if Number <= 50 Size is small, # if Number is between 50 and 70, Size is Medium # if Number is Bigger than 70, Size is Big ifelse (M$Number <=50, M

Using else with multiple if statements C#

点点圈 提交于 2019-12-08 01:59:27
问题 Is there a way to quickly check the following logic? I'm using C#. if(a) { } if(b) { } if(c) { } else none of the above //...? execute if all above conditions are false { } This differs from using if-else in that a, b, and c can all be true at once. So I can't stack them that way. I want to check else for a, b, and c are all false without writing if(!a && !b && !c) . This is because the code can get quite messy when the if conditions become more complex. It requires rewriting a lot of code.

Pandas and apply function to match a string

拥有回忆 提交于 2019-12-08 00:53:53
问题 I have a df column containing various links, some of them containing the string "search" . I want to create a function that - being applied to the column - returns a column containing "search" or "other" . I write a function like: search = 'search' def page_type(x): if x.str.contains(search): return 'Search' else: return 'Other' df['link'].apply(page_type) but it gives me an error like: AttributeError: 'unicode' object has no attribute 'str' I guess I'm missing something when calling the str

How to rename elements of a column based on conditional statements?

馋奶兔 提交于 2019-12-07 17:20:26
问题 In pandas, given this data frame: df = pd.DataFrame({'l':['a','b','a','c','b','b','a','b','b','a'], 'v':['x','x','y','y','y','x','x','y','x','y'],'n':[1,2,1,2,2,1,2,1,1,2], 'g':[0,0,0,0,0,1,1,1,1,1]}) What is the best solution to rename the elements of v based on some conditional statements applied to the data frame? Basically, for each row (regardless of whether g == 0 or g == 1 ): if df.l==a and df.n==1: df.v='val1' elif df.l==a and df.n==2: df.v='val2' elif df.l==b and df.n==1: df.v='val3'

What condition is false in a multiple conditions if statement

只愿长相守 提交于 2019-12-07 16:05:24
问题 I'm writing a multiple if statement in Javascript. I've 3 (or more) conditions, and I wanna doSomething() only if all these 3 conditions are true. If only one of these 3 are false, I wanna doSomethingElse(). I think my code it's right, but my problem is on another level. What if I wanna know for which condition my statement is false? E.g.: condition1=true, condition2=true, condition3=false. if (condition1 && condition2 && condition3) { doSomething(); } else { doSomethingElse(); }; I've

Anyway to shorten if ( i == x || i == y)?

孤街醉人 提交于 2019-12-07 10:55:59
问题 I tried to shorten my code, from : if(i== x || i == y || i == z ) to if (i == ( x || y || z )) I know this way is wrong because I got incorrect i in log. However, is there any method to shorten the code in objective-C ? 回答1: if there is a higher probability that x==i than y==i then it's better to write it as x==i || y==i as opposed to y==i || x==i because if the first statement evaluates to true, the second one is not evaluated (it's shortcircuited) 回答2: You could use a switch statement, but

Python code for sum with condition

强颜欢笑 提交于 2019-12-07 10:03:54
问题 The task is following: sum the list elements with even indexes and multiply the result by the last list's elemet. I have this oneliner solution code in Python. array = [-37,-36,-19,-99,29,20,3,-7,-64,84,36,62,26,-76,55,-24,84,49,-65,41] print sum(i for i in array if array.index(i) % 2 == 0)*array[-1] if array != [] else 0 My result is -1476 ( The calculation is: 41*(-37-19+29+3-64+36+26+55+84-65) ) The right result is 1968. I can't figure it out why this code is not working correctly in this

Check for consecutive numbers

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-07 07:42:01
问题 I have an array m of integers. I'm looking for a method to check if the elements of m are consecutive. Is there a way to test for consecutive numbers? I came up with this code intended to work when the array length is four: m.count == 4 && (m.max-m.min) == 3 which incorrectly returns true for [1,1,1,4] or [0,0,0,3] . 回答1: Enumerable has a really handy method called each_cons that works like this: [1,2,3,4].each_cons(2).to_a # => [ [1, 2], [2, 3], [3, 4] ] That is, it yields each consecutive

Generic List, items counting with conditional-statement

可紊 提交于 2019-12-07 06:49:25
问题 I have a Generic List. it has a ListfilesToProcess.Count property which returns total number of items, but I want to count certain number of items in list with conditional-statement. I am doing it like this: int c = 0; foreach (FilesToProcessDataModels item in ListfilesToProcess) { if (item.IsChecked == true) c++; } Is there any shorter way like int c = ListfilesToProcess.count(item => item.IsChecked == true); 回答1: Yes, use LINQ's Count method, with the overload taking a predicate: int count

Why is there no comparison statement in this javascript 'If… Else…' statement

耗尽温柔 提交于 2019-12-07 05:46:40
问题 In the following code var $next = $active.next().length ? $active.next() : $('#slideshow IMG:first'); the part '$active.next().length' doesn't seem to compare anything and I don't understand how the condition is determined to be True or False. Or is it saying that: if the various $next is equal to $active.next().length then the condition is true? 回答1: In javascript any expression can be converted to a truthy or falsy value and hence is valid in a comparison place. The values which are false