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

Python: Kwargs

**kwargs and *args are function arguments that can be very useful.

They are quite underused and often under-understood as well.

Let's try to explain what kwargs are and how to use them.

  • While *args are used to pass arguments at an unknown amount to functions, **kwargs are used to do the same but with named arguments.
  • So, if *args is a list being passed as an argument, you can think of **kwargs as a dictionary that's being passed as an argument to a function.
  • You can use arguments as you wish as long as you follow the correct order which is: arg1, arg2, *args, **kwargs. It's okay to use only one of those but you can't mix the order, for instance, you can't have: function(**kwargs, arg1), that'd be a major faux pas in Python.
  • Another example: You can do function(*args,**kwargs) since it follows the correct order.
  • Here is an example. Let's say satelites are given with their names and weight in tons in dictionary format. Code prints their weight as kilograms along with their names.
def payloads(**kwargs):
    for key, value in kwargs.items():
        print( key+" |||", float(value)*100)
payloads(NavSat1 = '2.5', BaysatG2 = '4')

Output:

NavSat1 ||| 250.0
BaysatG2 ||| 400.0

Since the function above would work for any number of dictionary keys, **kwargs makes perfect sense rather than passing arguments with a fixed amount.

def payloads(**kwargs):
    for key, value in kwargs.items():
        print( key+" |||", float(value)*100)

sats={"Tx211":"3", "V1":"0.50"}
payloads(**sats)

Output:

Tx211 ||| 300.0
V1 ||| 50.0