[HELP] Guided Project: Ebay Car Sales Data

I have attached a screenshot from Jupyter with code that I believe should have worked but didn’t.

I used the solution key code, and this code worked. I am unsure as to why. I tend to prefer using the .loc when using boolean operators, and I have tried it multiple different ways without being able to get it to work successfully.

Please help me understand! I understand what is going on with the solution key code. I just don’t understand why the code I wrote didn’t fulfill the same function.

If you can’t view my screenshot, here is my incorrect code:
autos = autos.loc[autos.loc[:, ‘registration_year’].between(1910,2016), ‘registration_year’]

And here is the solution key code:
autos = autos[autos[“registration_year”].between(1900,2016)]

Does your incorrect code produce an error? If so, can you share the full error?

I don’t have the full error info any longer, but I changed my original code from:
autos = autos.loc[autos.loc[:, ‘registration_year’].between(1910,2016), ‘registration_year’]

to: autos = autos[autos.loc[:, ‘registration_year’].between(1910,2016)]

This helped me get the same thing as the solution key. In my original, incorrect, code I’m assuming that I ran the boolean through the column registration_year. Instead, I should have been running my boolean through the entire dataframe. So using the .loc was the wrong choice from the start, I should have simply not specified the column.

Please let me know if my thoughts are correct. I have to review my understanding about how to index with booleans, but I think I indexed one too many times through the same column when I should be indexing through the dataframe as a whole.

It’s quite hard to understand exactly what happened when you ran that code without the error, so unfortunately I can’t suggest what might have happened. I have a suspicion that your inner .loc[] might have returned a dataframe when you expected it to return a series, but I can’t be certain.