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.
I like to explore, experiment and write about data science concepts and tools. You could connect with me on LinkedIn.


