Daily Dose of Data Science

Share this post

Speed-up Pandas Apply 5x with NumPy

www.blog.dailydoseofds.com

Discover more from Daily Dose of Data Science

High-quality insights on Data Science and Python, along with best practices — shared daily. Get a 550+ Page Data Science PDF Guide and 450+ Practice Questions Notebook, FREE.
Over 36,000 subscribers
Continue reading
Sign in

Speed-up Pandas Apply 5x with NumPy

Avi Chawla
Jan 1, 2023
1
Share this post

Speed-up Pandas Apply 5x with NumPy

www.blog.dailydoseofds.com
Share

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 conditions and the corresponding results are passed as the first two arguments. The last argument is the default result.

Read more here: NumPy docs.

Share this post on LinkedIn: Post Link.

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.


Find the code for my tips here: GitHub.

I like to explore, experiment and write about data science concepts and tools. You can read my articles on Medium. Also, you can connect with me on LinkedIn.

1
Share this post

Speed-up Pandas Apply 5x with NumPy

www.blog.dailydoseofds.com
Share
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