Filtering DataFrames in pandas: .loc[] vs. bracket notation While learning Python and pandas, I realized that both .loc[] and the bracket (shorthand) notation are used for boolean indexing. At first, they may look interchangeable, but there is an important difference in how and when to use each one. 🔹 Bracket (shorthand) boolean filtering df[df['age'] > 30] This is the most concise way to filter a DataFrame using a boolean condition. It’s quick, readable, and commonly used during exploration or simple filtering tasks. 🔹 Boolean indexing with .loc[] df.loc[df['age'] > 30, ['name', 'age']] Here, .loc[] explicitly states: * which rows meet the condition * which columns should be selected or modified Because of that, .loc[] offers more control and reduces ambiguity — especially when you need to update values safely or work with larger, more complex DataFrames. So what’s the real difference? Both approaches filter data using Boolean logic, but: ✔️ Bracket notation is great for quick filtering and inspection ✔️ .loc[] is preferred when: * selecting rows and columns together * modifying values without triggering unexpected behavior * writing clearer, more explicit, and maintainable code 📌 Key takeaway: The difference isn’t about what they do, it’s about how explicit and safe your code is. Using .loc[] makes your intent clearer and helps avoid subtle issues as your codebase grows. I’d love to hear from you: What have you learned about filtering in pandas? Do you have a preferred approach or any tips when working with Boolean indexing in Python? #python #pandas #dataanalytics #learningpython #datascience
Whenever possible, I stick to bracket-based filtering. It’s more readable — not just for others, but also for my future self trying to understand what I was thinking.
Nesse cenário também podemos utilizar o método .query(), não? df.query('age > 30')[['name','age']]