One of the projects in the Python Pandas course asks you to create a new column called
is_click , which is True if the value in the column
ad_click_timestamp is not null and
False otherwise. The project can be found at the link below.
One solution is:
ad_clicks['is_click'] = ad_clicks.ad_click_timestamp.notnull()
But I was wondering if there was a way to do this with
.apply() and a lambda function. I realize it is not as simple a solution, but to build my coding knowledge I wanted to figure it out.
Here is what I tried:
ad_clicks['is_click'] = ad_clicks.apply(lambda row: True if row.ad_click_timestamp is not null else False, axis=1)
It produces a NameError saying null is not defined.
I also tried this:
ad_clicks['is_click'] = ad_clicks.apply(lambda row: True if row.ad_click_timestamp.notnull() else False, axis=1)
But it produces an AttributeError saying str object has no attribute notnull() which makes sense to me because I am accessing a string value in some cases.