Pandas apply but only for rows where a condition is met

The other answers are excellent, but I thought I’d add one other approach that can be faster in some circumstances – using broadcasting and masking to achieve the same result: import numpy as np mask = (z[‘b’] != 0) z_valid = z[mask] z[‘c’] = 0 z.loc[mask, ‘c’] = z_valid[‘a’] / np.log(z_valid[‘b’]) Especially with very large … Read more

Trouble passing in lambda to apply for pandas DataFrame

Note there is no axis param for a Series.apply call, as distinct to a DataFrame.apply call. Series.apply(func, convert_dtype=True, args=(), **kwds) func : function convert_dtype : boolean, default True Try to find better dtype for elementwise function results. If False, leave as dtype=object args : tuple Positional arguments to pass to function in addition to the … Read more

Why is `vapply` safer than `sapply`?

As has already been noted, vapply does two things: Slight speed improvement Improves consistency by providing limited return type checks. The second point is the greater advantage, as it helps catch errors before they happen and leads to more robust code. This return value checking could be done separately by using sapply followed by stopifnot … Read more

How can I call a javascript constructor using call or apply? [duplicate]

This is how you do it: function applyToConstructor(constructor, argArray) { var args = [null].concat(argArray); var factoryFunction = constructor.bind.apply(constructor, args); return new factoryFunction(); } var d = applyToConstructor(Date, [2008, 10, 8, 00, 16, 34, 254]); Call is slightly easier function callConstructor(constructor) { var factoryFunction = constructor.bind.apply(constructor, arguments); return new factoryFunction(); } var d = callConstructor(Date, 2008, … Read more

What’s the .apply jQuery function?

apply calls a function with a set of arguments. It’s not part of jQuery, it’s part of core Javascript. However, there is mention of it in the jQuery docs: http://docs.jquery.com/Types#Context.2C_Call_and_Apply Syntax: somefunction.apply(thisObj, [argsArray]) The above calls the function somefunction, setting this to thisObj within the function’s scope, and passing in the arguments from argsArray as … Read more

Why are loops slow in R?

It’s not always the case that loops are slow and apply is fast. There’s a nice discussion of this in the May, 2008, issue of R News: Uwe Ligges and John Fox. R Help Desk: How can I avoid this loop or make it faster? R News, 8(1):46-50, May 2008. In the section “Loops!” (starting … Read more

How does the Math.max.apply() work?

apply accepts an array and it applies the array as parameters to the actual function. So, Math.max.apply(Math, list); can be understood as, Math.max(“12”, “23”, “100”, “34”, “56”, “9”, “233”); So, apply is a convenient way to pass an array of data as parameters to a function. Remember console.log(Math.max(list)); # NaN will not work, because max … Read more

R Apply() function on specific dataframe columns

lapply is probably a better choice than apply here, as apply first coerces your data.frame to an array which means all the columns must have the same type. Depending on your context, this could have unintended consequences. The pattern is: df[cols] <- lapply(df[cols], FUN) The ‘cols’ vector can be variable names or indices. I prefer … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)