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