When I have two filters in a dataframe, can I combine them in one step of code or have to do it step by step?

Good evening guys,
I am working on Data Cleaning Walkthrough: Combining the Data 9/18.

The instruction says:
* Filter graduation , only selecting rows where the Cohort column equals 2006 .
* Filter graduation , only selecting rows where the Demographic column equals Total Cohort .

So my code went like this and it didn’t work.
data['graduation'] = data['graduation'][data['graduation']['Cohort'] == '2006' & data['graduation']['Demographic'] == 'Total Cohort']

The solution is like this:
data[“graduation”] = data[“graduation”][data[“graduation”][“Cohort”] == “2006”]
data[“graduation”] = data[“graduation”][data[“graduation”][“Demographic”] == “Total Cohort”]

So my question is:
if we set up a filter on different columns but within the same dataframe, can we combine them in one step, essentially one line of code? Or the output could be quite different if we do filters step by step?

Thank you for your explanation!

Yep, you can absolutely filter on multiple columns in one line of code. It’s just a matter of how long, messy, and convoluted you’re willing to let your code get before you decide that it’d be easier for everyone to understand (including your future self!) if you just split the code into multiple lines. :rofl:

I listed one example of this in another recent thread:
df[(df['player_age'] > 25) & (df['goals_scored] == 2)]

Remember the parentheses around each column filter! This is what I think was missing in your original answer.

In essence, what I did in the example was:

  1. Apply filters over multiple columns (kept distinct by the parentheses),
  2. Got the intersect of rows that were common among these filtered columns using “&”, and then
  3. Filtered the dataframe (df) to only return the rows whose columns met the conditions outlined.

You’d just have to extend that same principle here:

data['graduation'] = data['graduation'][(data['graduation']['Cohort'] == '2006') & (data['graduation']['Demographic'] == 'Total Cohort')]

Let me know if the above works.

3 Likes

@blueberrypudding85 Thank you so much. It really works. And also thank you for all the tips on how to write a concise and easy to read code.

1 Like