Pandas DataFrame Head Method

PythonPythonBeginner
Practice Now

Introduction

In this lab, we will learn how to use the head() method in the Python pandas library to get the first n rows of a DataFrame. This method is commonly used to quickly inspect the data in a DataFrame and understand its structure.

VM Tips

After the VM startup is done, click the top left corner to switch to the Notebook tab to access Jupyter Notebook for practice.

Sometimes, you may need to wait a few seconds for Jupyter Notebook to finish loading. The validation of operations cannot be automated because of limitations in Jupyter Notebook.

If you face issues during learning, feel free to ask Labby. Provide feedback after the session, and we will promptly resolve the problem for you.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL python(("`Python`")) -.-> python/BasicConceptsGroup(["`Basic Concepts`"]) pandas(("`Pandas`")) -.-> pandas/DataSelectionGroup(["`Data Selection`"]) python(("`Python`")) -.-> python/DataStructuresGroup(["`Data Structures`"]) python(("`Python`")) -.-> python/ModulesandPackagesGroup(["`Modules and Packages`"]) python(("`Python`")) -.-> python/DataScienceandMachineLearningGroup(["`Data Science and Machine Learning`"]) python(("`Python`")) -.-> python/FunctionsGroup(["`Functions`"]) python/BasicConceptsGroup -.-> python/comments("`Comments`") pandas/DataSelectionGroup -.-> pandas/select_columns("`Select Columns`") python/DataStructuresGroup -.-> python/lists("`Lists`") python/DataStructuresGroup -.-> python/tuples("`Tuples`") python/DataStructuresGroup -.-> python/dictionaries("`Dictionaries`") python/ModulesandPackagesGroup -.-> python/importing_modules("`Importing Modules`") python/DataScienceandMachineLearningGroup -.-> python/numerical_computing("`Numerical Computing`") python/DataScienceandMachineLearningGroup -.-> python/data_analysis("`Data Analysis`") python/FunctionsGroup -.-> python/build_in_functions("`Build-in Functions`") subgraph Lab Skills python/comments -.-> lab-68632{{"`Pandas DataFrame Head Method`"}} pandas/select_columns -.-> lab-68632{{"`Pandas DataFrame Head Method`"}} python/lists -.-> lab-68632{{"`Pandas DataFrame Head Method`"}} python/tuples -.-> lab-68632{{"`Pandas DataFrame Head Method`"}} python/dictionaries -.-> lab-68632{{"`Pandas DataFrame Head Method`"}} python/importing_modules -.-> lab-68632{{"`Pandas DataFrame Head Method`"}} python/numerical_computing -.-> lab-68632{{"`Pandas DataFrame Head Method`"}} python/data_analysis -.-> lab-68632{{"`Pandas DataFrame Head Method`"}} python/build_in_functions -.-> lab-68632{{"`Pandas DataFrame Head Method`"}} end

Import the pandas library

First, we need to import the pandas library using the import statement.

import pandas as pd

Create a DataFrame

Next, we'll create a DataFrame object that contains some sample data. For example, let's create a DataFrame with a single column "Language" that contains a list of programming languages.

## Create the DataFrame
df = pd.DataFrame({'Language': ['Kannada','Hindi', 'Telugu', 'Tamil', 'Malyalam','Marathi','Konkani','Tulu']})

Use the head() method

To get the first n rows of the DataFrame, we can use the head() method. By default, it returns the first 5 rows. Here's an example:

print("----First 5 rows of the DataFrame is-----")
print(df.head())

Specify the number of rows

We can also specify the number of rows we want to retrieve by passing a parameter to the head() method. This allows us to get a specific number of rows from the beginning of the DataFrame. For example, to get the first 2 rows, we can use the following code:

print("----First 2 rows of the DataFrame is-----")
print(df.head(n=2))

Use a negative value for n

If we pass a negative value for n to the head() method, it will return all rows except the last n rows. This is equivalent to using slicing notation df[:-n]. Here's an example:

print("----First n rows of the DataFrame is-----")
print(df.head(-2))

Summary

In this lab, we learned how to use the head() method in the pandas library to retrieve the first n rows of a DataFrame. This method is useful for quickly inspecting the data and understanding its structure. By specifying the number of rows, we can retrieve a specific subset of the DataFrame. Additionally, using a negative value for n allows us to retrieve all rows except the last n rows. Overall, the head() method is a handy tool for exploring the contents of a DataFrame.

Other Python Tutorials you may like