Don't Create Conditional Columns in Pandas with Apply
While creating conditional columns in Pandas, we tend to use the 𝐚𝐩𝐩𝐥𝐲() method almost all the time.
However, 𝐚𝐩𝐩𝐥𝐲() in Pandas is nothing but a glorified for-loop. As a result, it misses the whole point of vectorization.
Instead, you should use the 𝐧𝐩.𝐰𝐡𝐞𝐫𝐞() method to create conditional columns. It does the same job but is extremely fast.
The condition is passed as the first argument. This is followed by the result if the condition evaluates to True (second argument) and False (third argument).
Read more here: NumPy docs.
Thanks for reading Daily Dose of Data Science! Subscribe for free to learn something new and insightful about Python and Data Science every day. Also, get a Free Data Science PDF (250+ pages) with 200+ tips.
I like to explore, experiment and write about data science concepts and tools. You could connect with me on LinkedIn.