Instagram
youtube
Facebook
Twitter

Select the First 5 Rows of a Pandas DataFrame

Select the First 5 Rows of a Pandas DataFrame
     
Short Description of the Program:

This program reads a CSV file into a Pandas DataFrame and then selects the first 5 rows using the .head() function. 
     

Explanation:

  • pd.read_csv('data.csv')
    Loads the CSV data into a DataFrame.

     
  • df.head()
    Returns the first 5 rows of the DataFrame. If you want a different number, you can pass a value like df.head(3).

     

Program:

import pandas as pd

df = pd.read_csv('data.csv')

first_five_rows = df.head()

print(first_five_rows)