Machine Learning with Python
67.8K subscribers
1.48K photos
127 videos
197 files
1.2K links
Learn Machine Learning with hands-on Python tutorials, real-world code examples, and clear explanations for researchers and developers.

Admin: @HusseinSheikho || @Hussein_Sheikho
Download Telegram
My favorite way to work with multiple filters in pandas.Series — not a chain of .loc, but a single mask. 🐼

The chain looks neat, but breaks on real data and easily gives unexpected results:

s = pd.Series([10, 15, 20, 25, 30])
s.loc[s > 20].loc[s % 2 == 1]

The problem is that the second .loc again looks at the original s, not the already filtered result. The logic gets messy. 🤯

It's more reliable to gather everything into one expression:

s = pd.Series([10, 15, 20, 25, 30])

mask = (s > 20) & (s % 2 == 1)
result = s.loc[mask]

One mask, one point of truth.

It's easier to debug. Fewer surprises when the code grows. 🚀

#Pandas #Python #DataScience #CodingTips #DataEngineering #Debugging
2