How do i print the first few rows in python?

To get the first N rows of a Pandas DataFrame, use the function pandas.DataFrame.head(). You can pass an optional integer that represents the first N rows. If you do not pass any number, it returns the first 5 rows. Meaning, the default N is 5.

Example 1: DataFrame.head(N)

In this example, we will get the first 3 rows of the DataFrame.

Python Program

import pandas as pd

#initialize a dataframe
df = pd.DataFrame(
	[[21, 72, 67],
	[23, 78, 62],
	[32, 74, 56],
	[73, 88, 67],
	[32, 74, 56],
	[43, 78, 69],
	[32, 74, 54],
	[52, 54, 76]],
	columns=['a', 'b', 'c'])

#get first 3 rows
df1 = df.head(3)

#print the dataframe
print(df1)

Run

Output

How do i print the first few rows in python?

Example 2: DataFrame.head()

In this example, we will not pass any number to the function head(). By default, head() function returns first 5 rows.

Python Program

import pandas as pd

#initialize a dataframe
df = pd.DataFrame(
	[[21, 72, 67],
	[23, 78, 62],
	[32, 74, 56],
	[73, 88, 67],
	[32, 74, 56],
	[43, 78, 69],
	[32, 74, 54],
	[52, 54, 76]],
	columns=['a', 'b', 'c'])

#get first default number of rows
df1 = df.head()

#print the dataframe
print(df1)

Run

Output

How do i print the first few rows in python?

Summary

In this Pandas Tutorial, we extracted the first N rows of Pandas DataFrame, using pandas.DataFrame.head() method, with the help of well detailed Python example programs.

  • Pandas iterrows() – Iterate over DataFrame Rows
  • How to iterate over Elements of Row in Pandas DataFrame?
  • How to Filter Pandas DataFrame Rows?
  • How to Add or Insert Row to Pandas DataFrame?
  • How to Count Rows in Pandas DataFrame?

Last update on August 19 2022 21:51:42 (UTC/GMT +8 hours)

Pandas: IMDb Movies Exercise-9 with Solution

Write a Pandas program to display the first 10 rows of the DataFrame.

Sample Solution:

Python Code :

import pandas as pd
df = pd.read_csv('movies_metadata.csv')
#Display the first 10 rows
result = df.head(10)
print("First 10 rows of the DataFrame:")
print(result)

Sample Output:

First 10 rows of the DataFrame:
   adult    ...     vote_count
0  False    ...           5415
1  False    ...           2413
2  False    ...             92
3  False    ...             34
4  False    ...            173
5  False    ...           1886
6  False    ...            141
7  False    ...             45
8  False    ...            174
9  False    ...           1194

[10 rows x 24 columns]                                     

Python-Pandas Code Editor:

Sample Table:

Have another way to solve this solution? Contribute your code (and comments) through Disqus.

Previous: Write a Pandas program to create a smaller DataFrame with a subset of all features.
Next: Write a Pandas program to sort the DataFrame based on release_date.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.

Python: Tips of the Day

Union Of Collections:

To get a distinct combined set of two sets.

a = {1,2,3}
b = {3,4,5}
c = a.union(b)


  • Exercises: Weekly Top 16 Most Popular Topics
  • SQL Exercises, Practice, Solution - JOINS
  • SQL Exercises, Practice, Solution - SUBQUERIES
  • JavaScript basic - Exercises, Practice, Solution
  • Java Array: Exercises, Practice, Solution
  • C Programming Exercises, Practice, Solution : Conditional Statement
  • HR Database - SORT FILTER: Exercises, Practice, Solution
  • C Programming Exercises, Practice, Solution : String
  • Python Data Types: Dictionary - Exercises, Practice, Solution
  • Python Programming Puzzles - Exercises, Practice, Solution
  • C++ Array: Exercises, Practice, Solution
  • JavaScript conditional statements and loops - Exercises, Practice, Solution
  • C# Sharp Basic Algorithm: Exercises, Practice, Solution
  • Python Lambda - Exercises, Practice, Solution
  • Python Pandas DataFrame: Exercises, Practice, Solution
  • Conversion Tools
  • JavaScript: HTML Form Validation


How do I print the first 5 rows in Python?

DataFrame. head(n) to get the first n rows of the DataFrame. It takes one optional argument n (number of rows you want to get from the start). By default n = 5, it return first 5 rows if value of n is not passed to the method.

How do I print the top 5 rows in Python?

head() prints the indexes. dataframe. to_string(index=False,max_rows=10) prints the first 5 and last 5 rows. Omit print() , use only ndf.

How do you get the first 3 rows in Python?

So to get first three rows of the dataframe, we can assign the value of n as '3'..
Syntax: Dataframe.head(n).
Syntax: dataframe.iloc[statrt_index, end_index+1].
Syntax: Dataframe.iloc [ [m,n] ].

How do you print certain rows in Python?

You can use the df. loc[[2]] to print a specific row of a pandas dataframe.