Daily Dose of Data Science

Share this post

Don't Create Conditional Columns in Pandas with Apply

www.blog.dailydoseofds.com

Don't Create Conditional Columns in Pandas with Apply

Avi Chawla
Oct 28, 2022
5
Share
Share this post

Don't Create Conditional Columns in Pandas with Apply

www.blog.dailydoseofds.com

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.

5
Share
Share this post

Don't Create Conditional Columns in Pandas with Apply

www.blog.dailydoseofds.com
Previous
Next
Comments
Top
New
Community

No posts

Ready for more?

Β© 2023 Avi Chawla
Privacy βˆ™ Terms βˆ™ Collection notice
Start WritingGet the app
SubstackΒ is the home for great writing