How to Get the Head of a Pandas Dataframe Object in Python


In this article, we show how to get the head of a pandas dataframe object in Python.

The head of a dataframe object is the first few entries (or rows) of the dataframe object.

The head is obtained many times to get a view of the columns of a dataframe object, as well as a few entries of the dataframe.

And we can get the head of the dataframe simply by using the head() function.

We use the name of the dataframe followed by a dot and the head() function.

This is shown in the following code below.

So the first thing we have to do is import seaborn as sns. We import as sns because it's an abbreviated shorthand way of referrign to the seaborn module.

We then load a built-in dataframe from seaborn with the load_dataset() function.

One of the built-in datas ets is the tips data set. We get this data set with the load_dataset() function.

We then show the full data set.

You can see that this data set is 244 rows long.

When examining a data set, you may not want to see all these rows. You may just want to see a certain amount of rows, not the whole thing.

By default, when you call the head() function in Python, it shows the first 5 rows of the dataframe, which is what you see above.

However, you don't have to always see the first 5 rows. You may want to see just the first row or the first 3 rows. Or you may want to see the first 10 rows. To do this, use the head() function and specify the number of rows that you would like returned.

In the following code below, we show the first 3 rows of the tips dataframe object using the head() function and specifying 3 as its parameter.

So we again use the head() function in the code above, but this time we specify how many rows we want returned by putting in an integer as a parameter to the head() function.

In the first example, we use the statement, tips.head(3)

This returns the first 3 rows of the tips dataframe object.

In the second example, we use the statement, tips.head(10)

This returns the first 10 rows of the tips dataframe object.

And this is how to get the head of a pandas dataframe ojbect in Python.

Related Resources

How to Randomly Select From or Shuffle a List in Python

HTML Comment Box is loading comments...