Python pandas apply function if a column value is not NULL
The problem is that pd.notnull(['foo', 'bar'])
operates elementwise and returns array([ True, True], dtype=bool)
. Your if condition trys to convert that to a boolean, and that's when you get the exception.
To fix it, you could simply wrap the isnull statement with np.all
:
df[['A','C']].apply(lambda x: my_func(x) if(np.all(pd.notnull(x[1]))) else x, axis = 1)
Now you'll see that np.all(pd.notnull(['foo', 'bar']))
is indeed True
.
Also another way is to just use row.notnull().all()
(without numpy
), here is an example:
df.apply(lambda row: func1(row) if row.notnull().all() else func2(row), axis=1)
Here is a complete example on your df:
>>> d = {'A': [None, 2, 3, 4], 'B': [11, None, 33, 4], 'C': [None, ['a','b'], None, 4]}>>> df = pd.DataFrame(d)>>> df A B C0 NaN 11.0 None1 2.0 NaN [a, b]2 3.0 33.0 None3 4.0 4.0 4>>> def func1(r):... return 'No'...>>> def func2(r):... return 'Yes'...>>> df.apply(lambda row: func1(row) if row.notnull().all() else func2(row), axis=1)0 Yes1 Yes2 Yes3 No
And a friendlier screenshot :-)