I was going to use a lambda function that returns 'Purchase'
for not null values and returns 'No Purchase'
else. I am not be sure which one to use. If I use the first one it works. However, I can't understand why I have to use the first one instead of the second one.
df['is_purchase'] = df.click_day.apply(lambda x: 'Purchase' if pd.notnull(x) else 'No Purchase')
df['is_purchase'] = df.click_day.apply(lambda x: 'Purchase' if pd.notnull(x)==True else 'No Purchase')
Can someone please explain why the first one is true?
Because if pd.notnull(x)==True
is bad style, in any programming language. The ==True
is already implied, so why include it?
If something
has the logical value True
, then the result of comparison it with True
:
something == True
is True
, too.
The opposite is true, too: If something == True
, then something
has the True
logical value. So the form
if something:
means the same as
if something == True:
To understand why the shortest form is preferred, compare
if (a < 10) == True:
with
if a < 10:
Which one is more natural?
Here's a visual example of what has already been explained:
In [45]: s = pd.Series([True, False])
In [46]: s
Out[46]:
0 True
1 False
dtype: bool
In [47]: s2 = s == True
In [48]: s2
Out[48]:
0 True
1 False
dtype: bool
In [49]: s.equals(s2)
Out[49]: True
Saying if some_bool
is the same as saying if some_bool == True
, so there's no need to type it out explicitly.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.