Amazing technological breakthrough possible @S-Logix pro@slogix.in

Office Address

  • #5, First Floor, 4th Street Dr. Subbarayan Nagar Kodambakkam, Chennai-600 024 Landmark : Samiyar Madam
  • pro@slogix.in
  • +91- 81240 01111

Social List

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.

Process

   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.

Sample Code

#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))

Screenshots
use head and tail function in Python pandas
import pandas library
head method,by default its print first 5 rows in a data set
to print rows by passing arguments
print rows by passing arguments