What is Pandas Data Filtering? See Examples

When working with large datasets, you rarely need all the data at once.
πŸ‘‰ That’s where data filtering comes in.

Filtering allows you to:

  • Extract specific rows
  • Apply conditions
  • Focus only on relevant data

This is one of the most frequently used operations in Pandas.


πŸ“Œ What is Data Filtering?

Data filtering means selecting a subset of data based on conditions.

Think of it like:

  • Applying filters in Excel
  • Writing SQL WHERE clauses

πŸ”Ž Basic Filtering

πŸ“Œ Filter Rows Based on Condition

df[df["Age"] > 23]

πŸ‘‰ Returns only rows where age is greater than 23.


πŸ“Œ Filter with Equal Condition

df[df["Name"] == "Sagar"]

πŸ”— Multiple Conditions

AND Condition (&)

df[(df["Age"] > 22) & (df["City"] == "Delhi")]

OR Condition (|)

df[(df["Age"] > 24) | (df["City"] == "Mumbai")]

NOT Condition (~)

df[~(df["City"] == "Delhi")]

🎯 Filtering Specific Columns

df[["Name", "Age"]]

πŸ‘‰ Combine with filtering:

df[df["Age"] > 23][["Name", "Age"]]

🧠 Using loc and iloc

πŸ“ Using loc (Label-based)

df.loc[df["Age"] > 23, ["Name", "City"]]

πŸ“ Using iloc (Position-based)

df.iloc[0:2]

πŸ” Advanced Filtering

πŸ”Ή Using isin()

df[df["City"].isin(["Delhi", "Mumbai"])]

πŸ”Ή Using between()

df[df["Age"].between(22, 25)]

πŸ”Ή Using str.contains()

df[df["Name"].str.contains("a")]

πŸ‘‰ Useful for text filtering.


⚑ Real-World Example

import pandas as pddf = pd.read_csv("employees.csv")# Employees with high salary
high_salary = df[df["Salary"] > 50000]# Employees from IT department
it_employees = df[df["Department"] == "IT"]# Combined filter
filtered = df[(df["Salary"] > 50000) & (df["Department"] == "IT")]print(filtered)

πŸš€ Performance Tip

Use .query() for cleaner syntax:

df.query("Age > 23 and City == 'Delhi'")

πŸ‘‰ Makes your code more readable.


🚫 Common Mistakes

  • ❌ Forgetting parentheses in multiple conditions
  • ❌ Using and instead of &
  • ❌ Using or instead of |
  • ❌ Not handling case sensitivity in strings

🎯 Why Filtering is Important

Filtering helps you:

  • Focus on important data
  • Reduce processing time
  • Improve analysis accuracy

🌐 External Resources


🏁 Conclusion

Data filtering is one of the most essential skills in Pandas.
Once mastered, you can quickly extract insights from even the largest datasets.

πŸ‘‰ Practice different conditions and combinations to become confident.


πŸ”– Hashtags

#Pandas #DataFiltering #Python #DataAnalysis #Coding #MachineLearning #AI #Developers #LearnPython #Analytics

Leave a Comment

Comments

No comments yet. Why don’t you start the discussion?

Leave a Reply

Your email address will not be published. Required fields are marked *