python

How to Get First 5 Rows of DataFrame in Python


In this article, you will learn how to get the first 5 rows of DataFrame in Python.

Let’s say you have a DataFrame of colours.

# Import pandas module
import pandas as pd

df = pd.DataFrame({"Colours": ['Red', 'Orange', 'Yellow', 'Green',
                   'Blue', 'Indigo', 'Violet']})

In order to get the first 5 rows of DataFrame, you can use the DataFrame.head() method.

# Import pandas module
import pandas as pd

df = pd.DataFrame({"Colours": ['Red', 'Orange', 'Yellow', 'Green',
                   'Blue', 'Indigo', 'Violet']})

print(df.head())

Note: The DataFrame.head() method functions by returning the first 5 rows of DataFrame by default.


Share on social media

//