Please note, this is a STATIC archive of website www.w3resource.com from 19 Jul 2022, cach3.com does not collect or store any user information, there is no "phishing" involved.
w3resource

Pandas Indexing: Exercises, Practice, Solution

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

Pandas Indexing [ 26 exercises with solution]

1. Write a Pandas program to display the default index and set a column as an Index in a given dataframe. Go to the editor
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. Write a Pandas program to create a multi Index frame using two columns and using an Index and a column. Go to the editor
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. 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. Go to the editor
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. Write a Pandas program to create an index labels by using 64-bit integers, using floating-point numbers in a given dataframe. Go to the editor
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. Write a Pandas program to create a DataFrame using intervals as an index. Go to the editor
Click me to see the sample solution

6. Write a Pandas program to create a dataframe indexing by date and time. Go to the editor
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. Write a Pandas program to create a dataframe and set a title or name of the index column. Go to the editor
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. Write a Pandas program to set value in a specific cell in a given dataframe using index. Go to the editor
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. Write a Pandas program to convert index of a given dataframe into a column. Go to the editor
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. 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. Go to the editor
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. Write a Pandas program to check if a specified value exists in single and multiple column index dataframe. Go to the editor
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. Write a Pandas program to construct a series using the MultiIndex levels as the column and index. Go to the editor
Click me to see the sample solution

13. Write a Pandas program to construct a DataFrame using the MultiIndex levels as the column and index. Go to the editor
Click me to see the sample solution

14. Write a Pandas program to extract a single row, rows and a specific value from a MultiIndex levels DataFrame. Go to the editor
Click me to see the sample solution

15. Write a Pandas program to rename names of columns and specific labels of the Main Index of the MultiIndex dataframe. Go to the editor
Click me to see the sample solution

16. Write a Pandas program to sort a MultiIndex of a DataFrame. Also sort on various levels of index. Go to the editor
Click me to see the sample solution

17. Write a Pandas program to extract elements in the given positional indices along an axis of a dataframe. Go to the editor
Click me to see the sample solution

18. Write a Pandas program to get the index of an element of a given Series. Go to the editor
Click me to see the sample solution

19. Write a Pandas program to select a specific row of given series/dataframe by integer index. Go to the editor
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. Write a Pandas program to find the indexes of rows of a specified value of a given column in a DataFrame. Go to the editor
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. Write a Pandas program to drop a index level from a multi-level column index of a dataframe. Go to the editor
Click me to see the sample solution

22. Write a Pandas program to insert a column at a specific index in a given DataFrame. Go to the editor
Click me to see the sample solution

23. Write a Pandas program to print a DataFrame without index. Go to the editor
Click me to see the sample solution

24. Write a Pandas program to find integer index of rows with missing data in a given dataframe. Go to the editor
Click me to see the sample solution

25. Write a Pandas program to start index with different value rather than 0 in a given DataFrame. Go to the editor
Click me to see the sample solution

26. Write a Pandas program to select rows by filtering on one or more column(s) in a multi-index dataframe. Go to the editor
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



Python: Tips of the Day

Find current directory and file's directory:

To get the full path to the directory a Python file is contained in, write this in that file:

import os 
dir_path = os.path.dirname(os.path.realpath(__file__))

(Note that the incantation above won't work if you've already used os.chdir() to change your current working directory, since the value of the __file__ constant is relative to the current working directory and is not changed by an os.chdir() call.)

To get the current working directory use

import os
cwd = os.getcwd()

Documentation references for the modules, constants and functions used above:

  • The os and os.path modules.
  • The __file__ constant
  • os.path.realpath(path) (returns "the canonical path of the specified filename, eliminating any symbolic links encountered in the path")
  • os.path.dirname(path) (returns "the directory name of pathname path")
  • os.getcwd() (returns "a string representing the current working directory")
  • os.chdir(path) ("change the current working directory to path")

Ref: https://bit.ly/3fy0R6m