w3resource

Pandas: Selecting a row of series/dataframe by integer index

Pandas Indexing: Exercise-19 with Solution

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

Sample Solution:

Python Code :

import pandas as pd
ds = pd.Series([1,3,5,7,9,11,13,15], index=[0,1,2,3,4,5,7,8])
print("Original Series:")
print(ds)
print("\nPrint specified row from the said series using location based indexing:")
print("\nThird row:")
print(ds.iloc[[2]])
print("\nFifth row:")
print(ds.iloc[[4]])
df = pd.DataFrame({
    'school_code': ['s001','s002','s003','s001','s002','s004'],
    'class': ['V', 'V', 'VI', 'VI', 'V', 'VI'],
    'name': ['Alberto Franco','Gino Mcneill','Ryan Parkes', 'Eesha Hinton', 'Gino Mcneill', 'David Parkes'],
    'date_of_birth': ['15/05/2002','17/05/2002','16/02/1999','25/09/1998','11/05/2002','15/09/1997'],
    'weight': [35, 32, 33, 30, 31, 32]})

print("Original DataFrame with single index:")
print(df)
print("\nPrint specified row from the said DataFrame using location based indexing:")
print("\nThird row:")
print(df.iloc[[2]])
print("\nFifth row:")
print(df.iloc[[4]])

Sample Output:

Original Series:
0     1
1     3
2     5
3     7
4     9
5    11
7    13
8    15
dtype: int64

Print specified row from the said series using location based indexing:

Third row:
2    5
dtype: int64

Fifth row:
4    9
dtype: int64
Original DataFrame with single index:
  school_code class            name date_of_birth  weight
0        s001     V  Alberto Franco    15/05/2002      35
1        s002     V    Gino Mcneill    17/05/2002      32
2        s003    VI     Ryan Parkes    16/02/1999      33
3        s001    VI    Eesha Hinton    25/09/1998      30
4        s002     V    Gino Mcneill    11/05/2002      31
5        s004    VI    David Parkes    15/09/1997      32

Print specified row from the said DataFrame using location based indexing:

Third row:
  school_code class         name date_of_birth  weight
2        s003    VI  Ryan Parkes    16/02/1999      33

Fifth row:
  school_code class          name date_of_birth  weight
4        s002     V  Gino Mcneill    11/05/2002      31

Python Code Editor:


Have another way to solve this solution? Contribute your code (and comments) through Disqus.

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

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

What is the difficulty level of this exercise?

Test your Python skills with w3resource's quiz



Python: Tips of the Day

How to sort a Python dict by value

Example:

x1 = {'a': 5, 'b': 7, 'c': 9, 'd': 1}

sorted(x1.items(), key=lambda x: x[1])
[('d', 1), ('c', 9), ('b', 7), ('a', 5)]

# Or:

import operator
print(sorted(x1.items(), key=operator.itemgetter(1)))

Output:

[('d', 1), ('a', 5), ('b', 7), ('c', 9)]