Pandas apply but only for rows where a condition is met Pandas apply but only for rows where a condition is met python python

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 npmask = (z['b'] != 0)z_valid = z[mask]z['c'] = 0z.loc[mask, 'c'] = z_valid['a'] / np.log(z_valid['b'])

Especially with very large dataframes, this approach will generally be faster than solutions based on apply().


You can just use an if statement in a lambda function.

z['c'] = z.apply(lambda row: 0 if row['b'] in (0,1) else row['a'] / math.log(row['b']), axis=1)

I also excluded 1, because log(1) is zero.

Output:

   a  b         c0  4  6  2.2324431  5  0  0.0000002  6  5  3.7280103  7  0  0.0000004  8  1  0.000000


Hope this helps. It is easy and readable

df['c']=df['b'].apply(lambda x: 0 if x ==0 else math.log(x))