Epython Lab
6.11K subscribers
680 photos
31 videos
104 files
1.31K links
Welcome to Epython Lab, where you can get resources to learn, one-on-one trainings on machine learning, business analytics, and Python, and solutions for business problems.

Buy ads: https://telega.io/c/epythonlab
Download Telegram
๐ˆ ๐ฌ๐ฉ๐ž๐ž๐ ๐ฎ๐ฉ ๐ฉ๐š๐ง๐๐š๐ฌ ๐›๐ฒ ๐Ÿ๐ŸŽ๐ŸŽ๐ฑ, ๐š๐ง๐ ๐ก๐ž๐ซ๐ž ๐ข๐ฌ ๐ž๐ฑ๐š๐œ๐ญ๐ฅ๐ฒ ๐ก๐จ๐ฐ ๐ฒ๐จ๐ฎ ๐œ๐š๐ง ๐ญ๐จ๐จ. ๐Ÿš€

If your DataFrame manipulation feels lightning-fast on a small sample but crawls to a halt on a million rows, you are likely falling into a few common performance traps.

Here are the 3 core optimizations that drove a massive speedup:



1๏ธโƒฃ ๐ƒ๐ข๐ญ๐œ๐ก .๐š’๐š๐šŽ๐š›๐š›๐š˜๐š ๐šœ() ๐Ÿ๐จ๐ซ ๐•๐ž๐œ๐ญ๐จ๐ซ๐ข๐ณ๐š๐ญ๐ข๐จ๐ง

Row-by-row looping forces Pandas to create a Series object for every row, bypassing C-level optimizations. Let NumPy and Pandas handle your array math natively.



โŒ ๐’๐ฅ๐จ๐ฐ (๐‘๐จ๐ฐ-๐›๐ฒ-๐ซ๐จ๐ฐ ๐ข๐ญ๐ž๐ซ๐š๐ญ๐ข๐จ๐ง):

๐š๐š˜๐š› ๐š’, ๐š›๐š˜๐š  ๐š’๐š— ๐š๐š.๐š’๐š๐šŽ๐š›๐š›๐š˜๐š ๐šœ():

๐š๐š.๐š•๐š˜๐šŒ[๐š’, "๐š๐š˜๐š๐šŠ๐š•"] = ๐š›๐š˜๐š ["๐š™๐š›๐š’๐šŒ๐šŽ"] * ๐š›๐š˜๐š ["๐šš๐šž๐šŠ๐š—๐š๐š’๐š๐šข"]



โœ… ๐Ÿ๐ŸŽ๐ŸŽ๐ฑ ๐…๐š๐ฌ๐ญ๐ž๐ซ (๐•๐ž๐œ๐ญ๐จ๐ซ๐ข๐ณ๐ž๐ ๐จ๐ฉ๐ž๐ซ๐š๐ญ๐ข๐จ๐ง):

๐š๐š["๐š๐š˜๐š๐šŠ๐š•"] = ๐š๐š["๐š™๐š›๐š’๐šŒ๐šŽ"] * ๐š๐š["๐šš๐šž๐šŠ๐š—๐š๐š’๐š๐šข"]



2๏ธโƒฃ ๐’๐ฎ๐ฉ๐ž๐ซ๐œ๐ก๐š๐ซ๐ ๐ž ๐Œ๐ฎ๐ฅ๐ญ๐ข-๐‚๐จ๐ฅ๐ฎ๐ฆ๐ง ๐Œ๐š๐ญ๐ก ๐ฐ๐ข๐ญ๐ก ๐š™๐š.๐šŽ๐šŸ๐šŠ๐š•()

# ๐™ด๐š•๐š’๐š–๐š’๐š—๐šŠ๐š๐šŽ๐šœ ๐š๐šŽ๐š–๐š™๐š˜๐š›๐šŠ๐š›๐šข ๐š–๐šŽ๐š–๐š˜๐š›๐šข ๐šŠ๐š•๐š•๐š˜๐šŒ๐šŠ๐š๐š’๐š˜๐š— ๐š˜๐šŸ๐šŽ๐š›๐š‘๐šŽ๐šŠ๐š

๐š๐š["๐š›๐šŽ๐šœ๐šž๐š•๐š"] = ๐š™๐š.๐šŽ๐šŸ๐šŠ๐š•("๐šŒ๐š˜๐š•๐Ÿท + ๐šŒ๐š˜๐š•๐Ÿธ * ๐šŒ๐š˜๐š•๐Ÿน - ๐šŒ๐š˜๐š•๐Ÿบ", ๐š๐šŠ๐š›๐š๐šŽ๐š=๐š๐š)



3๏ธโƒฃ ๐ƒ๐จ๐ฐ๐ง๐œ๐š๐ฌ๐ญ ๐ƒ๐š๐ญ๐š ๐“๐ฒ๐ฉ๐ž๐ฌ & ๐”๐ฌ๐ž ๐‚๐š๐ญ๐ž๐ ๐จ๐ซ๐ข๐œ๐š๐ฅ๐ฌ

By default, Pandas assigns 64-bit integers/floats and treats string columns as bloated ๐š˜๐š‹๐š“๐šŽ๐šŒ๐š types. Downcasting your numbers and converting repeating strings to ๐šŒ๐šŠ๐š๐šŽ๐š๐š˜๐š›๐šข can shrink your memory footprint by up to 80%โ€”drastically accelerating every subsequent operation.



# ๐™ณ๐š˜๐š ๐š—๐šŒ๐šŠ๐šœ๐š ๐š—๐šž๐š–๐šŽ๐š›๐š’๐šŒ๐šœ & ๐š–๐šŠ๐š™ ๐šœ๐š๐š›๐š’๐š—๐š๐šœ ๐š๐š˜ ๐šŒ๐šŠ๐š๐šŽ๐š๐š˜๐š›๐šข

๐š๐š["๐š’๐š"] = ๐š™๐š.๐š๐š˜_๐š—๐šž๐š–๐šŽ๐š›๐š’๐šŒ(๐š๐š["๐š’๐š"], ๐š๐š˜๐š ๐š—๐šŒ๐šŠ๐šœ๐š="๐š’๐š—๐š๐šŽ๐š๐šŽ๐š›")

๐š๐š["๐š›๐šŽ๐š๐š’๐š˜๐š—"] = ๐š๐š["๐š›๐šŽ๐š๐š’๐š˜๐š—"].๐šŠ๐šœ๐š๐šข๐š™๐šŽ("๐šŒ๐šŠ๐š๐šŽ๐š๐š˜๐š›๐šข")



๐Ÿ’ก ๐“๐ก๐ž ๐“๐š๐ค๐ž๐š๐ฐ๐š๐ฒ: Optimize your code at the vector and memory level before throwing expensive compute clusters or hardware upgrades at the problem.



What is your favorite performance trick for handling large datasets? Letโ€™s share notes in the comments below! ๐Ÿ‘‡



Explore more https://youtu.be/3vX1S2qxDu8



#Python #Pandas #DataScience #DataEngineering #CodeOptimization #MachineLearning #Programming