w3resource

Pandas DataFrame: Get the first 3 rows of a given DataFrame


4. Selecting the First 3 Rows

Write a Pandas program to get the first 3 rows of a given DataFrame.

Sample DataFrame:
exam_data = {'name': ['Anastasia', 'Dima', 'Katherine', 'James', 'Emily', 'Michael', 'Matthew', 'Laura', 'Kevin', 'Jonas'],
'score': [12.5, 9, 16.5, np.nan, 9, 20, 14.5, np.nan, 8, 19],
'attempts': [1, 3, 2, 3, 2, 3, 1, 1, 2, 1],
'qualify': ['yes', 'no', 'yes', 'no', 'no', 'yes', 'yes', 'no', 'no', 'yes']}
labels = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j']

Sample Solution :

Python Code :

import pandas as pd
import numpy as np

exam_data  = {'name': ['Anastasia', 'Dima', 'Katherine', 'James', 'Emily', 'Michael', 'Matthew', 'Laura', 'Kevin', 'Jonas'],
        'score': [12.5, 9, 16.5, np.nan, 9, 20, 14.5, np.nan, 8, 19],
        'attempts': [1, 3, 2, 3, 2, 3, 1, 1, 2, 1],
        'qualify': ['yes', 'no', 'yes', 'no', 'no', 'yes', 'yes', 'no', 'no', 'yes']}
labels = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j']

df = pd.DataFrame(exam_data , index=labels)
print("First three rows of the data frame:")
print(df.iloc[:3])

Sample Output:

First three rows of the data frame:                                    
   attempts       name qualify  score                                  
a         1  Anastasia     yes   12.5                                  
b         3       Dima      no    9.0                                  
c         2  Katherine     yes   16.5                                   

Explanation:

The above code creates a Pandas DataFrame named df with columns 'name', 'score', 'attempts', and 'qualify', and a custom index 'labels'. It then selects and prints the first three rows of the DataFrame using the .iloc indexing method.

Specifically, df.iloc[:3] selects the first three rows of the DataFrame using integer-based indexing, where : indicates all rows and 3 indicates up to the third row (exclusive). This operation returns a new DataFrame containing the selected rows, which is then printed using the print() function.


For more Practice: Solve these Related Problems:

  • Write a Pandas program to select the first 3 rows of a DataFrame and then output the indices in reverse order.
  • Write a Pandas program to select the first 3 rows and then duplicate these rows at the bottom of the DataFrame.
  • Write a Pandas program to select the first 3 rows and then filter out rows where a specific column’s value is NaN.
  • Write a Pandas program to select the first 3 rows and then convert these rows into a new DataFrame with reset index.

Python-Pandas Code Editor:

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

Previous: Write a Pandas program to display a summary of the basic information about a specified DataFrame and its data.
Next: Write a Pandas program to select the 'name' and 'score' columns from the following DataFrame.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.