w3resource

Pandas Indexing: Exercises, Practice, Solution


This resource offers a total of 130 Pandas Indexing problems for practice. It includes 26 main exercises, each accompanied by solutions, detailed explanations, and four related problems.

[An Editor is available at the bottom of the page to write and execute the scripts.]


1. Display Default and Set Column as Index

Write a Pandas program to display the default index and set a column as an Index in a given dataframe.

Test Data:

0        s001     V  Alberto Franco     15/05/2002      35  street1   t1
1        s002     V    Gino Mcneill     17/05/2002      32  street2   t2
2        s003    VI     Ryan Parkes     16/02/1999      33  street3   t3
3        s001    VI    Eesha Hinton     25/09/1998      30  street1   t4
4        s002     V    Gino Mcneill     11/05/2002      31  street2   t5
5        s004    VI    David Parkes     15/09/1997      32  street4   t6
Click me to see the sample solution


2. Create MultiIndex Frame

Write a Pandas program to create a multi Index frame using two columns and using an Index and a column.

Test Data:

0        s001     V  Alberto Franco     15/05/2002      35  street1   t1
1        s002     V    Gino Mcneill     17/05/2002      32  street2   t2
2        s003    VI     Ryan Parkes     16/02/1999      33  street3   t3
3        s001    VI    Eesha Hinton     25/09/1998      30  street1   t4
4        s002     V    Gino Mcneill     11/05/2002      31  street2   t5
5        s004    VI    David Parkes     15/09/1997      32  street4   t6   
Click me to see the sample solution


3. Reset Index After Setting Column as Index

Write a Pandas program to display the default index and set a column as an Index in a given dataframe and then reset the index.

Test Data:

0        s001     V  Alberto Franco     15/05/2002      35  street1   t1
1        s002     V    Gino Mcneill     17/05/2002      32  street2   t2
2        s003    VI     Ryan Parkes     16/02/1999      33  street3   t3
3        s001    VI    Eesha Hinton     25/09/1998      30  street1   t4
4        s002     V    Gino Mcneill     11/05/2002      31  street2   t5
5        s004    VI    David Parkes     15/09/1997      32  street4   t6
Click me to see the sample solution


4. Create 64-bit Integer and Float Index Labels

Write a Pandas program to create an index labels by using 64-bit integers, using floating-point numbers in a given dataframe.

Test Data:

0        s001     V  Alberto Franco     15/05/2002      35  street1   t1
1        s002     V    Gino Mcneill     17/05/2002      32  street2   t2
2        s003    VI     Ryan Parkes     16/02/1999      33  street3   t3
3        s001    VI    Eesha Hinton     25/09/1998      30  street1   t4
4        s002     V    Gino Mcneill     11/05/2002      31  street2   t5
5        s004    VI    David Parkes     15/09/1997      32  street4   t6
Click me to see the sample solution


5. DataFrame with Interval Index

Write a Pandas program to create a DataFrame using intervals as an index.

Click me to see the sample solution


6. DataFrame Indexed by Date and Time

Write a Pandas program to create a dataframe indexing by date and time.

Test Data:

0        s001     V  Alberto Franco     15/05/2002      35  street1   t1
1        s002     V    Gino Mcneill     17/05/2002      32  street2   t2
2        s003    VI     Ryan Parkes     16/02/1999      33  street3   t3
3        s001    VI    Eesha Hinton     25/09/1998      30  street1   t4
4        s002     V    Gino Mcneill     11/05/2002      31  street2   t5
5        s004    VI    David Parkes     15/09/1997      32  street4   t6
Click me to see the sample solution


7. Set Title for Index Column

Write a Pandas program to create a dataframe and set a title or name of the index column.

Test Data:

0        s001     V  Alberto Franco     15/05/2002      35  street1   t1
1        s002     V    Gino Mcneill     17/05/2002      32  street2   t2
2        s003    VI     Ryan Parkes     16/02/1999      33  street3   t3
3        s001    VI    Eesha Hinton     25/09/1998      30  street1   t4
4        s002     V    Gino Mcneill     11/05/2002      31  street2   t5
5        s004    VI    David Parkes     15/09/1997      32  street4   t6
Click me to see the sample solution


8. Set Value in Specific Cell using Index

Write a Pandas program to set value in a specific cell in a given dataframe using index.

Test Data:

0        s001     V  Alberto Franco     15/05/2002      35  street1   t1
1        s002     V    Gino Mcneill     17/05/2002      32  street2   t2
2        s003    VI     Ryan Parkes     16/02/1999      33  street3   t3
3        s001    VI    Eesha Hinton     25/09/1998      30  street1   t4
4        s002     V    Gino Mcneill     11/05/2002      31  street2   t5
5        s004    VI    David Parkes     15/09/1997      32  street4   t6
Click me to see the sample solution


9. Convert Index to Column

Write a Pandas program to convert index of a given dataframe into a column.

Test Data:

0        s001     V  Alberto Franco     15/05/2002      35  street1   t1
1        s002     V    Gino Mcneill     17/05/2002      32  street2   t2
2        s003    VI     Ryan Parkes     16/02/1999      33  street3   t3
3        s001    VI    Eesha Hinton     25/09/1998      30  street1   t4
4        s002     V    Gino Mcneill     11/05/2002      31  street2   t5
5        s004    VI    David Parkes     15/09/1997      32  street4   t6
Click me to see the sample solution


10. Convert 1st and 3rd Levels of MultiIndex to Columns

Write a Pandas program to convert 1st and 3rd levels in the index into columns from a multiple level of index frame of a given dataframe.

Test Data:

0        s001     V  Alberto Franco     15/05/2002      35  street1   t1
1        s002     V    Gino Mcneill     17/05/2002      32  street2   t2
2        s003    VI     Ryan Parkes     16/02/1999      33  street3   t3
3        s001    VI    Eesha Hinton     25/09/1998      30  street1   t4
4        s002     V    Gino Mcneill     11/05/2002      31  street2   t5
5        s004    VI    David Parkes     15/09/1997      32  street4   t6
Click me to see the sample solution


11. Check Value Existence in Index DataFrame

Write a Pandas program to check if a specified value exists in single and multiple column index dataframe.

Test Data:

0        s001     V  Alberto Franco     15/05/2002      35  street1   t1
1        s002     V    Gino Mcneill     17/05/2002      32  street2   t2
2        s003    VI     Ryan Parkes     16/02/1999      33  street3   t3
3        s001    VI    Eesha Hinton     25/09/1998      30  street1   t4
4        s002     V    Gino Mcneill     11/05/2002      31  street2   t5
5        s004    VI    David Parkes     15/09/1997      32  street4   t6
Click me to see the sample solution


12. Construct Series Using MultiIndex Levels as Column and Index

Write a Pandas program to construct a series using the MultiIndex levels as the column and index.

Click me to see the sample solution


13. Construct DataFrame Using MultiIndex Levels as Column and Index

Write a Pandas program to construct a DataFrame using the MultiIndex levels as the column and index.

Click me to see the sample solution


14. Extract from MultiIndex DataFrame

Write a Pandas program to extract a single row, rows and a specific value from a MultiIndex levels DataFrame.

Click me to see the sample solution


15. Rename Columns and Main Index Labels in MultiIndex DataFrame

Write a Pandas program to rename names of columns and specific labels of the Main Index of the MultiIndex dataframe.

Click me to see the sample solution


16. Sort MultiIndex of a DataFrame

Write a Pandas program to sort a MultiIndex of a DataFrame. Also sort on various levels of index.

Click me to see the sample solution


17. Extract Elements by Positional Indices

Write a Pandas program to extract elements in the given positional indices along an axis of a dataframe.

Click me to see the sample solution


18. Get Index of an Element in a Series

Write a Pandas program to get the index of an element of a given Series.

Click me to see the sample solution


19. Select Specific Row by Integer Index

Write a Pandas program to select a specific row of given series/dataframe by integer index.

Test Data:

0        s001     V  Alberto Franco     15/05/2002      35  street1   t1
1        s002     V    Gino Mcneill     17/05/2002      32  street2   t2
2        s003    VI     Ryan Parkes     16/02/1999      33  street3   t3
3        s001    VI    Eesha Hinton     25/09/1998      30  street1   t4
4        s002     V    Gino Mcneill     11/05/2002      31  street2   t5
5        s004    VI    David Parkes     15/09/1997      32  street4   t6
Click me to see the sample solution


20. Find Indexes of Rows for Specified Column Value

Write a Pandas program to find the indexes of rows of a specified value of a given column in a DataFrame.

Test Data:

0        s001     V  Alberto Franco     15/05/2002      35  street1   t1
1        s002     V    Gino Mcneill     17/05/2002      32  street2   t2
2        s003    VI     Ryan Parkes     16/02/1999      33  street3   t3
3        s001    VI    Eesha Hinton     25/09/1998      30  street1   t4
4        s002     V    Gino Mcneill     11/05/2002      31  street2   t5
5        s004    VI    David Parkes     15/09/1997      32  street4   t6
Click me to see the sample solution


21. Drop an Index Level from Multi-level Column Index

Write a Pandas program to drop a index level from a multi-level column index of a dataframe.

Click me to see the sample solution


22. Insert Column at Specific Index

Write a Pandas program to insert a column at a specific index in a given DataFrame.

Click me to see the sample solution


23. Print DataFrame Without Index

Write a Pandas program to print a DataFrame without index.

Click me to see the sample solution


24. Find Integer Index of Rows with Missing Data

Write a Pandas program to find integer index of rows with missing data in a given dataframe.

Click me to see the sample solution


25. Start Index with a Different Value

Write a Pandas program to start index with different value rather than 0 in a given DataFrame.

Click me to see the sample solution


26. Select Rows by Filtering in Multi-Index DataFrame

Write a Pandas program to select rows by filtering on one or more column(s) in a multi-index dataframe.

Click me to see the sample solution


Python 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



Follow us on Facebook and Twitter for latest update.