how to get first 5 rows of a dataframe
To get the first 5 rows of a dataframe, you normally use a built-in “head” method or slicing , depending on the library or language you’re using.
In Python (pandas DataFrame)
The most common case people mean by “dataframe” today is a pandas DataFrame.
python
import pandas as pd
# assume df is your DataFrame
first_five = df.head() # returns first 5 rows by default
# or explicitly
first_five = df.head(5)
You can also use slicing:
python
first_five = df[:5] # rows with index 0–4
In R (data.frame / tibble)
For base R data.frames:
r
head(df, 5) # first 5 rows
For tibbles (tidyverse), the same works:
r
head(df, 5)
In PySpark (Spark DataFrame)
python
df.show(5) # shows first 5 rows in console
first_five = df.take(5) # returns a list of Row objects
In SQL (table treated like a dataframe)
For many SQL dialects:
sql
SELECT * FROM your_table
LIMIT 5;
In SQL Server / some others:
sql
SELECT TOP 5 * FROM your_table;
HTML view of first 5 rows (pandas, in a notebook or web)
If you specifically want HTML (e.g., for a web page):
python
html_table = df.head(5).to_html()
This returns a string containing an HTML <table>...</table> that you can
embed in a webpage or render in a template.
TL;DR
- Pandas:
df.head()(ordf.head(5)ordf[:5]).
- R:
head(df, 5) - PySpark:
df.show(5) - SQL:
SELECT * FROM table LIMIT 5;
Information gathered from public forums or data available on the internet and portrayed here.