Print a specific row of a pandas DataFrame

In this article, we will discuss different ways to print a specific row of a pandas DataFrame.

Table of Contents

Preparing DataSet

To quickly get started, let’s create a sample dataframe to experiment. We’ll use the pandas library with some random data.

import pandas as pd

# List of Tuples
employees= [('Shubham', 'India', 'Tech',   5, 4),
            ('Riti', 'India', 'Design' ,   7, 7),
            ('Shanky', 'India', 'PMO' ,   2, 2),
            ('Shreya', 'India', 'Design' ,   2, 0),
            ('Aadi', 'US', 'PMO', 11, 5),
            ('Sim', 'US', 'Tech', 4, 4)]

# Create a DataFrame object from list of tuples
df = pd.DataFrame(employees,
                  columns=['Name', 'Location', 'Team', 'Experience', 'RelevantExperience'],
                  index = ['A', 'B', 'C', 'D', 'E', 'F'])
print(df)

Contents of the created dataframe are,

      Name Location    Team  Experience  RelevantExperience
A  Shubham    India    Tech           5                   4
B     Riti    India  Design           7                   7
C   Shanky    India     PMO           2                   2
D   Shreya    India  Design           2                   0
E     Aadi       US     PMO          11                   5
F      Sim       US    Tech           4                   4

In a pandas DataFrame, iloc is used to access any row or column based on their position. Let’s understand with an example, say, we need to print the row number 3 of the above DataFrame.

Advertisements
# print row 3
print (df.iloc[2])

Output

Name                  Shanky
Location               India
Team                     PMO
Experience                 2
RelevantExperience         2
Name: C, dtype: object

As observed, it has printed all the values along with their column header of row number 3. In case, we need only values, we can simply use the .values property here as below.

# print row 3
print (df.iloc[2].values)

Output

['Shanky' 'India' 'PMO' 2 2]

Another approach is to print any specific row based on the row name. Here, we will use the loc property of the pandas DataFrame.

# print row with name 'C'
print (df.loc['C'])

Output

Name                  Shanky
Location               India
Team                     PMO
Experience                 2
RelevantExperience         2
Name: C, dtype: object

As observed, it has returned a similar output as above. It is generally preferred when the index names are known, as calculating position might be an issue sometimes.

The complete example is as follows,

import pandas as pd

# List of Tuples
employees= [('Shubham', 'India', 'Tech',   5, 4),
            ('Riti', 'India', 'Design' ,   7, 7),
            ('Shanky', 'India', 'PMO' ,   2, 2),
            ('Shreya', 'India', 'Design' ,   2, 0),
            ('Aadi', 'US', 'PMO', 11, 5),
            ('Sim', 'US', 'Tech', 4, 4)]

# Create a DataFrame object from list of tuples
df = pd.DataFrame(employees,
                  columns=['Name', 'Location', 'Team', 'Experience', 'RelevantExperience'],
                  index = ['A', 'B', 'C', 'D', 'E', 'F'])
print(df)

# print row 3
print (df.iloc[2])

# print row with name 'C'
print (df.loc['C'])

Output:

      Name Location    Team  Experience  RelevantExperience
A  Shubham    India    Tech           5                   4
B     Riti    India  Design           7                   7
C   Shanky    India     PMO           2                   2
D   Shreya    India  Design           2                   0
E     Aadi       US     PMO          11                   5
F      Sim       US    Tech           4                   4

Name                  Shanky
Location               India
Team                     PMO
Experience                 2
RelevantExperience         2
Name: C, dtype: object

Name                  Shanky
Location               India
Team                     PMO
Experience                 2
RelevantExperience         2
Name: C, dtype: object

Summary

In this article, we have discussed how to print a specific row of a Pandas DataFrame. Thanks.

Advertisements

Thanks for reading.

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top