Slicing in Python

Hello, I was studying slicing in python and I have a question.

In this line of code temperatures_bool = temperatures[(temperatures[“date”] >= “2010-01-01”) & (temperatures[“date”] <= “2011-12-31”)]

Why do I have to repeat temperature inside the bracket? When I want to select a column it’s enough to code df[“column”], why isn’t it possible when slicing? Why can’t I code temperatures_bool=temperatures[(“date” >= “2010-01-01”) & (“date” <= “2011-12-31”)]

Thank you so much for your help


you are passing an array of booleans inside the brackets, "date" is a string, always the same string, so you can’t compare dataframe values with that. for the dataframe values you need to access the dataframe


This apples the comparison and creates a series of booleans - if you pass a series of booleans to a dataframe, it will slice it and create a DF only with the rows where the boolean was “True”.

Pandas does not apply this to the dataframe to check if either string actually refers to a column. Because both could be.


thank you so much. I understood it.

thanks both of you