Mastering advanced indexing and slicing in Pandas DataFrames
Pandas Advanced Indexing and Slicing [15 exercises with solution]
Advanced techniques for indexing, slicing, and selecting data in DataFrames, including multi-level indexing (MultiIndex). Practice with the following comprehensive exercises.
[An editor is available at the bottom of the page to write and execute the scripts. Go to the editor]
1. Write a Pandas program to select rows where the value in the 'A' column is greater than 4.
Click me to see the sample solution
2. Write a Pandas program to select only the 'X' and 'Y' columns from the DataFrame.
Click me to see the sample solution
3. Write a Pandas program to set a MultiIndex and access specific data using it.
Click me to see the sample solution
4. Write a Pandas program to slice DataFrame based on MultiIndex levels.
Click me to see the sample solution
5. Write a Pandas program to swap the levels of a MultiIndex DataFrame.
Click me to see the sample solution
6. Write a Pandas program to reset the index of a MultiIndex DataFrame.
Click me to see the sample solution
7. Write a Pandas program that uses .loc for indexing.
Click me to see the sample solution
8. Write a Pandas program to use Boolean indexing to select rows where column 'x' > 6.
Click me to see the sample solution
9. Write a Pandas program to select the first three rows using iloc.
Click me to see the sample solution
10. Write a Pandas program to use .loc to select rows based on a condition
Click me to see the sample solution
11. Write a Pandas program that uses .loc to set values in the DataFrame.
Click me to see the sample solution
12. Write a Pandas program that uses .loc to slice DataFrame based on row and column labels.
Click me to see the sample solution
13. Write a Pandas program to select rows where column 'X' > 5 and column 'Y' < 5.
Click me to see the sample solution
14. Write a Pandas program that uses .loc to slice a MultiIndex DataFrame.
Click me to see the sample solution
15. Write a Pandas program that uses MultiIndex to select data based on conditions.
Click me to see the sample solution
Python-Pandas Code Editor:
More to Come !
Do not submit any solution of the above exercises at here, if you want to contribute go to the appropriate exercise page.
Test your Python skills with w3resource's quiz
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics