Python head Function with Example Program

This tutorial will focus on broadening your knowledge of one of Python’s built-in functions which is the Python head with lots of example programs.

The strong ecosystem of data-centric Python packages makes Python an excellent language for data analysis. Pandas is one of these tools that simplify data import and analysis.

When we say data analysis, it is the action of analyzing data through functions. There are a lot of built-in functions in Python and Pandas packages. One of these is the head() function that enables programmers to analyze if the objects in the program are in the right type of data.

Also read: Python Set Union with Advanced Examples

What is a Python head?

In Python programming, the head() function is used to return the first five elements of data frames by default. However, if you want to display a specific number of elements, you may declare an n equal to a certain number (n = 6).

The head() function returns all rows except the last n rows for negative values of n, which is equivalent to df[:-n]. Thus, the value of n will come from the user and the program will display the first n elements (objects).

Python head() function is useful for testing if the objects on your program have the right data type.

Additionally, the head function has the following Syntax, Return Values, and Parameters:

Syntax:

DataFrame.head(self, n=5)

As mentioned beforehand, the n = 5 value is the default, but we can set the value for n specifically.

Parameters:

NameDescriptionTypeDefault ValueRequired or Optional
nNumbers of rows to select.Int5Required
Python head() Function Parameters

Returns

Python head() function returns values of n from head(n). This n will call the elements of the object’s first row.

For example:


import pandas

df = pandas.DataFrame({'animal': ['snake', 'bat', 'tiger', 'lion','fox', 'eagle'],
                       'food': ['cake', 'fried chicken', 'spaghetti', 'salad', 'asdasd', 'ghfghfgh']})
print(df.head(5))

Output:

     animal               food
0  snake                cake
1    bat             fried chicken
2  tiger              spaghetti
3   lion                    salad
4    fox                  asdasd

Explanation

From the example program, the function called the first five elements of two set objects (as assigned in the print function). Take a look at the illustrations below.

The first line of our program reads as:

df = pandas.DataFrame({'animal': ['snake', 'bat', 'tiger', 'lion', 'fox', 'eagle'],                        
                       'food': ['cake', 'fried chicken', 'spaghetti', 'salad', 'asdasd', 'ghfghfgh']}) 

Moreover, Python does not only have the head() as a built-in function but also has the tail(). You may also learn the Python endswith() function through the Python endswith() Method full tutorial.

What is the head () and tail () function?

Both head() and tail() are Python built-in functions that help programmers with data analysis.

The only difference between the two is that the head() function is applicable for returning the first n values in a row while tail() is for last. Therefore both function works similarly but differs in the position of n (index) of a row.

Python head list

For Python list, the head() function returns the first n values of a list. The number of its output will based on the assigned argument in the program.

However, as mentioned in the above topics, five(5) is the default value of the head() function. This parameter is required so your program will not encounter errors when executed.

df.shape in python

Apart from Python head() and tail(0 functions, you may also use the df.shape in Python. This function returns tuple of the shape of a data frame or series

This Python df.shape function returns a tuple with the number of rows and columns. Additionally, tuples can be unpacked and stored in distinct variables.

df.tail() in python

Python df.tail() function (also known as data frame tail function) is useful in calling the last n values of a list or object. This function can also easily determine the type of data after sorting or data appending.

Python df.tail() Syntax:

DataFrame.tail(self, n=5)

Parameters:

NameDescriptionTypeDefault ValueRequired or Optional
nNumbers of rows to select.Int5Required
Python df.tail() Function Parameters

Return Value:

Python df.tail() function returns the n value of the last portion of a series or objects (such as list, tuples, etc).

Summary

In summary, the Python head() function is not also usable for calling out the first n elements of a series r objects but also applicable for confirming what type of data is used in that object.

On the other hand, the head function has a corresponding opposite method which works the same with the head but applies at the last portion of a series or data frame. This method is known as the Python tail() function.

Both Python’s head and tail() functions have similarities when referring to how they work. Also, they are opposite particularly in the place where they apply their function.

Nevertheless, the Python head function tutorial has completed the overall topics in learning Python head. This also includes various examples which you can test and apply in your programs.

Hopefully, this tutorial has helped you in your Python programming journey and if you have any suggestions or clarifications, just leave it in the comments.

Have a nice day!

Leave a Comment