How to use head and tail function in Python pandas?

Description

To implement the head() and tail() functions for a pandas data frame using python.

   Import pandas library.

  Load the sample data set.

   We can pass the numbers(like df.head(8))also in that function.

   Store the data set in to pandas.

#import pandas library

import pandas as pd

#read data set from csv file

data=pd.read_csv(‘/home/soft27/soft27

/Sathish/Pythonfiles/Employee.csv’)

df=pd.DataFrame(data)

print(df)

#head method,by default its print first 5 rows in a data set.

print(“\n”)

print(“First 5 rows”)

print(df.head())

#to print rows by passing arguments.

print(“\n”)

print(“First 3 rows”)

print(df.head(3))

#tail method,by default its print last 5 rows in a data set

print(“\n”)

print(“Last 5 rows”)

print(df.tail())

#to print rows by passing arguments.

print(“\n”)

print(“Last 6 rows”)

print(df.tail(6))

Leave Comment

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

clear formSubmit