๐ ๐ฌ๐ฉ๐๐๐ ๐ฎ๐ฉ ๐ฉ๐๐ง๐๐๐ฌ ๐๐ฒ ๐๐๐๐ฑ, ๐๐ง๐ ๐ก๐๐ซ๐ ๐ข๐ฌ ๐๐ฑ๐๐๐ญ๐ฅ๐ฒ ๐ก๐จ๐ฐ ๐ฒ๐จ๐ฎ ๐๐๐ง ๐ญ๐จ๐จ. ๐
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
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