Selecting Specific Rows and Columns
Okay, you've dealt with the previous chapters, and now is the right time to combine your knowledge. You can specify both rows and columns; to do so, you just need to be familiar with the .loc[] attribute.
This function allows us to do plenty of different slicing operations, but for now, we will just consolidate knowledge from the previous chapters.
As usual, look at the example and then at the output.
data.loc[2:5, ['Director', 'ReleaseYear']]- outputs rows with the indices2,3,4,5(but remember that the indices start from0) from the columns'Director'and'ReleaseYear'(.loc[]includes the last index that you put into[]);data.loc[:5, ['Director', 'ReleaseYear']]- outputs rows with the indices0,1,2,3,4,5from the columns'Director'and'ReleaseYear';data.loc[997:, ['Director', 'ReleaseYear']]- outputs rows with the indices997,998,999(999is the index of the last row) from the columns'Director'and'ReleaseYear';data.loc[:, ['Director', 'ReleaseYear']]ordata[['Director', 'ReleaseYear']]- outputs all rows from the columns'Director'and'ReleaseYear'.
Swipe to start coding
Your task here is to output the necessary rows and columns. Follow the algorithm:
- Import the
pandaslibrary with thepdalias. - Read the csv file.
- Assign to
datavariable information about the columns'Title','Stars','Category'(in this order) with rows with indices from15to85. - Output the
data_extractedvariable.
Solution
Thanks for your feedback!
single
Ask AI
Ask AI
Ask anything or try one of the suggested questions to begin our chat
Can you explain more about how .loc[] works with both rows and columns?
What happens if I specify a row or column that doesn't exist using .loc[]?
Can you show more examples of slicing with .loc[]?
Awesome!
Completion rate improved to 3.03
Selecting Specific Rows and Columns
Swipe to show menu
Okay, you've dealt with the previous chapters, and now is the right time to combine your knowledge. You can specify both rows and columns; to do so, you just need to be familiar with the .loc[] attribute.
This function allows us to do plenty of different slicing operations, but for now, we will just consolidate knowledge from the previous chapters.
As usual, look at the example and then at the output.
data.loc[2:5, ['Director', 'ReleaseYear']]- outputs rows with the indices2,3,4,5(but remember that the indices start from0) from the columns'Director'and'ReleaseYear'(.loc[]includes the last index that you put into[]);data.loc[:5, ['Director', 'ReleaseYear']]- outputs rows with the indices0,1,2,3,4,5from the columns'Director'and'ReleaseYear';data.loc[997:, ['Director', 'ReleaseYear']]- outputs rows with the indices997,998,999(999is the index of the last row) from the columns'Director'and'ReleaseYear';data.loc[:, ['Director', 'ReleaseYear']]ordata[['Director', 'ReleaseYear']]- outputs all rows from the columns'Director'and'ReleaseYear'.
Swipe to start coding
Your task here is to output the necessary rows and columns. Follow the algorithm:
- Import the
pandaslibrary with thepdalias. - Read the csv file.
- Assign to
datavariable information about the columns'Title','Stars','Category'(in this order) with rows with indices from15to85. - Output the
data_extractedvariable.
Solution
Thanks for your feedback!
single