Articles

head and tail in pandas | head & tail function use, examples

head and tail in pandas | head & tail function use, examples


In this pandas tutorial, we will discuss about:

  • head in pandas dataframe,
  • pandas head example,
  • tail in pandas,
  • pandas tail example

Lets first create one dataframe and then we will use it to understand head and tail in pandas.

 

DataFrame in pandas is an two dimensional data structure that will store data in two dimensional format. One dimension refers to a row and second dimension refers to a column, So It will store the data in rows and columns.

We can able to create this DataFrame using DataFrame() method. But this is available in pandas module, so we have to import pandas module.

Syntax:

pandas.DataFrame(data)

Where, data is the input dataframe, The data can be a dictionary that stores list of values with specified key

 

Example: Create Dataframe

In this example, we will create a dataframe with 4 rows and 4 columns with college data and assign indices through index parameter.

import pandas as pd

#create dataframe from the college data
data= pd.DataFrame({'college_id':['c-001','c-021','c-002','c-004'],

                    'college_name':["vignan university","vvit","RVR - JC","Andhra University"],

                   "college_address":["guntur","guntur","guntur","guntur"],

                    "Total Staff":[1200,3422,5644,670]

                   },index=['one','two','three','four'])

#display the dataframe
print(data)

Output: Dataframe is created below

      college_id       college_name college_address Total Staff
one        c-001  vignan university          guntur        1200
two        c-021               vvit          guntur        3422
three      c-002           RVR - JC          guntur        5644
four       c-004  Andhra University          guntur         670

Now we will use this dataframe to apply methods head and tail in pandas and see the respective outputs.


head in pandas dataframe

head() or head function in dataframe pandas is used to get the top rows from the given pandas dataframe.

we can get n number of rows from top in the dataframe.

Syntax:

dataframe.head(n)

where, dataframe is the input pandas dataframe. It will take only one integer parameter n. It refers to number of rows to be displayed / returned from the top.

By default head in pandas will display top 5 rows.

Example: pandas head example

In this pandas head example, we will see head() function with different values.

import pandas as pd

#create dataframe from the college data
data= pd.DataFrame({'college_id':['c-001','c-021','c-002','c-004'],

                    'college_name':["vignan university","vvit","RVR - JC","Andhra University"],

                   "college_address":["guntur","guntur","guntur","guntur"],

                    "Total Staff":[1200,3422,5644,670]

                   },index=['one','two','three','four'])

#default
print(data.head())

print()

#top 3 rows
print(data.head(3))

print()

#top 1 row
print(data.head(1))

Outputpandas head example result

      college_id       college_name college_address  Total Staff
one        c-001  vignan university          guntur         1200
two        c-021               vvit          guntur         3422
three      c-002           RVR - JC          guntur         5644
four       c-004  Andhra University          guntur          670

      college_id       college_name college_address  Total Staff
one        c-001  vignan university          guntur         1200
two        c-021               vvit          guntur         3422
three      c-002           RVR - JC          guntur         5644

    college_id       college_name college_address  Total Staff
one      c-001  vignan university          guntur         1200

Apply your understanding from this pandas head example on other dataframes to develop better knowledge on the topic head in pandas.


tail in pandas

tail() function or pandas tail function is used to get the bottom rows from the given pandas dataframe.

we can get n number of rows from the bottom in the dataframe.

Syntax:

dataframe.tail(n)

where, dataframe is the input pandas dataframe. It will take only one integer parameter n. It refers to number of rows to be displayed / returned from the bottom.

 

By default tail in pandas will display bottom 5 rows.

 

Example: pandas tail example

In this pandas tail example, we will see tail() function with different values.

import pandas as pd

#create dataframe from the college data
data= pd.DataFrame({'college_id':['c-001','c-021','c-002','c-004'],

                    'college_name':["vignan university","vvit","RVR - JC","Andhra University"],

                   "college_address":["guntur","guntur","guntur","guntur"],

                    "Total Staff":[1200,3422,5644,670]

                   },index=['one','two','three','four'])

#default
print(data.tail())

print()

#bottom 3 rows
print(data.tail(3))

print()

#bottom 1 row
print(data.tail(1))

Outputpandas tail example result

      college_id       college_name college_address  Total Staff
one        c-001  vignan university          guntur         1200
two        c-021               vvit          guntur         3422
three      c-002           RVR - JC          guntur         5644
four       c-004  Andhra University          guntur          670

      college_id       college_name college_address  Total Staff
two        c-021               vvit          guntur         3422
three      c-002           RVR - JC          guntur         5644
four       c-004  Andhra University          guntur          670

     college_id       college_name college_address  Total Staff
four      c-004  Andhra University          guntur          670

You can see based on the values provided as parameter in tail in pandas, we get the respective results as output.

 

This wraps our chapter on head and tail in pandas.


Pandas

Would you like to see your article here on tutorialsinhand. Join Write4Us program by tutorialsinhand.com

About the Author
Gottumukkala Sravan Kumar 171FA07058
B.Tech (Hon's) - IT from Vignan's University. Published 800+ Technical Articles on Python, R, Java, C#, LISP, PHP - MySQL and Machine Learning
Page Views :    Published Date : Mar 29,2022  
Please Share this page

Related Articles

Like every other website we use cookies. By using our site you acknowledge that you have read and understand our Cookie Policy, Privacy Policy, and our Terms of Service. Learn more Got it!