How to Get first N rows of Pandas DataFrame in Python

In this Python Pandas tutorial, will learn how to get the first N rows of DataFrame using Pandas in Python. Also, we will cover these topics.

  • Get the first 10 rows of Pandas DataFrame
  • Get the first N row of Pandas DataFrame as list
  • Get the first N row of Pandas DataFrame as dictionary
  • Get the first N row of Pandas DataFrame as header
  • Get the first N row of Pandas DataFrame as column name
  • Get the first N row of Pandas DataFrame all columns

Get first N rows of Pandas DataFrame

  • In this Program, we will discuss how to get the first N rows of Pandas DataFrame.
  • In Python, the Pandas head() method is used to retrieve the first N number of rows of data from Pandas DataFrame. This function always returns all rows except the last n rows.
  • This method contains only one argument which is n and if you do not pass any number in the function then by default it will return the first 5 rows element.

Syntax:

Here is the Syntax of DataFrame.head() method

DataFrame.head
              (
               n=5
              )
  • It Consists of only one parameter
    • N: This parameter indicates the number of rows to return in the output. By default, this is set to n=5 and it will return the first 5 rows of data.

Example:

Let’s take an example and check how to get the first N rows of a Pandas DataFrame.

Source Code:

import pandas as pd

df = pd.DataFrame(
	[[692,128, 992],
	[139, 459, 167],
	[132, 907, 107],
	[2209, 552, 129],
	[3389, 719, 4038],
	[389, 154, 645],
	[378, 298, 114],
	[1, 2,6]],
	columns=['Germany', 'Newzealand', 'China'])

new_val = df.head()

print(new_val)

In the above code first, we have created a DataFrame that contains the values in the list. Now we want to extract the data of only 5 rows. To do this task we will use the head() function and it will return the first 5 rows.

Here is the execution of the following given code

Get the first N rows of Pandas DataFrame
Get the first N rows of Pandas DataFrame

Read Python Pandas CSV Tutorial

Get first N rows of Python Pandas DataFrame

  • By using the iloc() method we can also get the first N rows of Pandas DataFrame.
  • In Python, the iloc() method enables us to select a specific cell of the Dataframe and we can also retrieve a specific value belonging to a column and row.
  • This function takes only integer type values and also accesses all the data values.

Syntax:

Here is the Syntax of iloc() method

Pandas.DataFrame.iloc()

Source Code:

import pandas as pd

student_info = [('Micheal',    12, 'Japan',   223),
            ('George',   23, 'China' ,   129),
            ('John',   31, 'U.S.A' ,   458),
            ('Brenda',  18, 'France' ,  875),
            ('William', 27, 'Paris',   569),
            ('Olijah',  29, 'Uganda' ,   809)]

df = pd.DataFrame( student_info, 
                    columns=['Student_name', 'Stu_age', 'Stu_city', 'Student_id'])

N = 2
new_result = df.iloc[:N]
print("N rows of Pandas DataFrame: ")
print(new_result)

In the above program first, we have created a list of tuples in which we have assigned N no of rows. Now use the iloc() method and select the first 2 rows of the dataframe as a Pandas Dataframe object.

Here is the Output of the following given code

Get the first N rows of Pandas DataFrame
Get the first N rows of Pandas DataFrame

Read Missing Data in Pandas in Python

Get first 10 rows of Pandas DataFrame

  • Here we can see how to get the first 10 rows of Pandas DataFrame.
  • In this program, we have pass ’10’ as an argument in df.head() function. To return the first 10 rows we can use DataFrame.head().
  • This method is used to return 10 rows of a given DataFrame or series. You can also change the value between the parenthesis to change the number of rows.

Example:

import pandas as pd

df = pd.DataFrame(
	[[692,128, 992],
	[139, 459, 167],
	[132, 907, 107],
	[2209, 552, 129],
	[3389, 719, 4038],
	[389, 154, 645],
	[378, 298, 114],
	[1, 2,6],
    [231,178,190],
    [134,298,115],
    [219,884,721],
    [745,240,558]],
	columns=['Micheal', 'Oliva', 'Elijah'])

new_val = df.head(10)
print(new_val)

In the above code first, we have created a DataFrame that contains the values in the list. Now we want to extract the data of only 10 rows. To perform this task we will use the head() function and it will return the first 10 rows.

Here is the Screenshot of the following given code

Get the first 10 rows of Pandas DataFrame
Get the first 10 rows of Pandas DataFrame

Read Crosstab in Python Pandas

Get first N row of Pandas DataFrame as list

  • Let us see how to get the first N rows of Pandas DataFrame as list.
  • In this Program, we have created a list of tuples and assign values to it. Now create a DataFrame and pass the column name along with an index.

Source Code:

import pandas as pd
employee_info = [('Potter',    12, 'Australia',   223),
            ('George',   23, 'China' ,   339),
            ('John',   31, 'Germany' ,   992),
            ('Brenda',  18, 'Bangladesh' , 129),
            ('William', 27, 'France',   569),
            ('Olijah',  29, 'Uganda' ,   809)]
empDfObj = pd.DataFrame(employee_info, columns=['employee_name', 'emp_age', 'emp_city', 'emp_id'], index=['1', 'm', 'n', '2', '5', 'p'])

new_val = empDfObj.head(2)
print("First 2 rows of the Pandas Dataframe : ")
print(new_val)

In the above code, we have used the df. head() function and get the number of rows to return in the output. Once you will print ‘new_val’ then the output will display only 2 rows that are available in the list.

You can refer to the below Screenshot

Get the first N row of Pandas DataFrame as list
Get the first N row of Pandas DataFrame as a list

Read Python Pandas Write DataFrame to Excel

Get first N row of Pandas DataFrame as dictionary

  • In this Program, we will get the first N row of Pandas DataFrame as a dictionary.
  • In Python to perform this particular task first, we will create a dictionary and assigned key-value pair elements. In this example, the key element is considered as column name and similar values are considered as a list.

Source Code:

import pandas as pd

my_new_dict = {'Student_name' : ['Liah', 'Oliva',
				'Elijah', 'Micheal',
				'George'],
		'Student_age':[34,12,24,34,25],
		'Student_id':[167, 823, 456, 782, 912]}
df = pd.DataFrame(my_new_dict)
new_val = df.head(2)

print(new_val)

In the above program, we have created a DataFrame object and pass a dictionary as an argument. After that use DataFrame.head() function and assign an integer value that means Once you will print the ‘new_val’ then the output will display only two rows of given DataFrame.

Here is the implementation of the following given code

Get the first N row of Pandas DataFrame as dictionary
Get the first N row of Pandas DataFrame as a dictionary

Read Count Rows in Pandas DataFrame

Get first N row of Pandas DataFrame as header

  • Let us see how to get first N row of Pandas DataFrame as header.
  • By using DataFrame.iloc() method we can solve this problem and this method helps the user to select a specific cell of the Dataframe and we can also retrieve a specific value belonging to a column and row.

Example:

import pandas as pd

student_info = [('James',    17, 'Japan',   223),
            ('George',   18, 'China' ,   129),
            ('Adam',   23, 'U.S.A' ,   458),
            ('Hayden',  18, 'France' ,  875),
            ('Gilchrist', 27, 'Paris',   569),
            ('Olijah',  29, 'Uganda' ,   809)]

df = pd.DataFrame( student_info, 
                    columns=['Student_name', 'Stu_age', 'Stu_city', 'Student_id'])

N = 3
new_result = df.iloc[:N]
print("N rows of Pandas DataFrame with header: ")
print(new_result)

In the above code, we have created a DataFrame object which contains column names. Now use the iloc() method along with the slicing method [:]and select the first 2 rows of the dataframe as a Pandas Dataframe object.

Here is the Screenshot of the following given code

Get the first N row of Pandas DataFrame as header
Get the first N row of Pandas DataFrame as header

Read Python Pandas DataFrame Iterrows

Get first N row of Pandas DataFrame as column name

  • In this Program, we will discuss how to get the first N row of Pandas DataFrame as a column name.
  • By using a dataframe.head() method we can easily get the N number rows of Pandas DataFrame with the column name.

Source Code:

import pandas as pd

df = pd.DataFrame(
	[[321,462, 198],
	[923, 210, 875],
	[132, 907, 107],
	[2209, 552, 129],
	[3389, 719, 4038],
	[389, 154, 645],
	[378, 298, 114],
	[211, 32,689]],
	columns=['Newyork', 'England', 'U.S.A'])

new_val = df.head(3)

print(new_val)

In this example, we have created a list of column names and then use a dataframe.head() method to get the specific rows of a dataframe.

Here is the implementation of the following given code

Get the first N row of Pandas DataFrame as column name
Get the first N row of Pandas DataFrame as column name

Read Python convert DataFrame to list

Get first N row of Pandas DataFrame all columns

  • In this program, we will discuss how to get first N row of Pandas DataFrame columns.
  • To perform this task we can apply the method DataFrame.head() and use all the columns in the list.

Source Code:

import pandas as pd
employee_info = [('c',    34, 'Newyork',   223),
            ('G',   26, 'C' ,   339),
            ('h',   11, 'china' ,   992),
            ('l',  18, 'Bangladesh' , 129),
            ('n', 25, 'France',   569),
            ('O',  21, 'Uganda' ,   809)]
result = pd.DataFrame(employee_info, columns=['alphabet', 'emp_age', 'emp_city', 'emp_id'])

new_val = result.head(3)
print("First 2 rows of the Pandas Dataframe : ")
print(new_val)

You can refer to the below Screenshot

Get the first N row of Pandas DataFrame all columns
Get the first N row of Pandas DataFrame all columns

You may like the following Python Pandas tutorial:

In this Python Pandas tutorial, will learn how to get the first N rows of DataFrame using Pandas. Also, we will cover these topics.

  • Get the first 10 rows of Pandas DataFrame
  • Get the first N row of Pandas DataFrame as list
  • Get the first N row of Pandas DataFrame as dictionary
  • Get the first N row of Pandas DataFrame as header
  • Get the first N row of Pandas DataFrame as column name
  • Get the first N row of Pandas DataFrame all columns