CYBER WEEK - EXTRA SAVINGS EVENT
TRY A FREE LESSON

Https://app.dataquest.io/m/1033/selecting-data-in-dataframes-practice-problems/17/selecting-rows-and-columns

https://app.dataquest.io/m/1033/selecting-data-in-dataframes-practice-problems/17/selecting-rows-and-columns

Could someone help in explaining the generic syntax that is being used in DQ solution:

odd_rows_weight_height = people[1::2][[‘Weight’, ‘Height’]]

For example to select a subset, dataframe[rows:columns] is the syntax that i am aware of
I am also aware of loc and iloc methods.

Hi @raturi22. I’m guessing that your confusion stems from the slicing notation, specifically what does [1::2]do? Check out this Stack Overflow post that explains the notation.

thanks Mike. The confusion is also with respect to how the two sets of brackets are put together - next to one another. It is not intuitive. I could not have guessed it. I understand that odd rows will be selected using df[1::2] and columns through df[[‘Weight’, ‘Height’]], it’s the combination which is troubling.

I came across another practice problem on another screen. This looks kind of intuitive as it follows [rows, columns] pattern. This one has labels instead of indexes , is there a way to rewrite the earlier one in this way?

my_selection = people.loc[‘013’:‘087’:2,[‘Name’, ‘Country’]]

Most definitely! This is the way I did it the first time:

odd_rows_weight_height = people.loc[1::2, ['Weight', 'Height']]

That said, there is nothing wrong with doing it the other way too:

odd_rows_weight_height = people[1::2][[‘Weight’, ‘Height’]]

And the way to break this down is to think of it as a two step process: first you select the odd rows using [1::2] and are returned a new df with only odd rows, then you apply column indexing on this new df using [['Weight', 'Height']] to give you the final desired result.