Pandas Tutorial
Creating Objects
Viewing Data
Selection
Manipulating Data
Grouping Data
Merging, Joining and Concatenating
Working with Date and Time
Working With Text Data
Working with CSV and Excel files
Operations
Visualization
Applications and Projects
Viewing the top rows of a DataFrame is often the first step in understanding the structure and content of your dataset when working with pandas. The head()
method in pandas allows you to easily inspect the top rows.
Here's a tutorial on how to view the top rows of a DataFrame using pandas:
Ensure you have pandas installed:
pip install pandas
import pandas as pd
data = { 'A': [1, 2, 3, 4, 5, 6, 7, 8, 9, 10], 'B': [10, 9, 8, 7, 6, 5, 4, 3, 2, 1], 'C': list(range(10, 20)) } df = pd.DataFrame(data) print(df)
By default, the head()
method displays the top 5 rows:
print(df.head())
However, you can specify a different number of rows by passing an integer argument to the head()
method:
print(df.head(3)) # Displays the top 3 rows
The head()
method returns the first n
rows from the DataFrame. By default, it returns 5 rows, but you can specify any number by passing it as an argument.
It's especially useful when dealing with large datasets, as it allows you to quickly inspect the first few entries without loading the entire dataset into memory.
The head()
method in pandas is a quick and easy way to view the initial rows of a DataFrame. It's one of the most frequently used methods in exploratory data analysis to get a sense of the data you're working with. Another complementary method is tail()
, which shows you the last n
rows of the DataFrame.
View first rows of DataFrame in Pandas:
head()
function to display the initial rows of the DataFrame.first_rows = df.head()
Displaying top records in Pandas DataFrame:
head()
method.top_records = df.head(10) # Display top 10 rows
Pandas show first n rows:
head()
function.first_n_rows = df.head(5) # Display first 5 rows
How to use head() in Pandas:
head()
method as a quick way to inspect the initial rows.preview_data = df.head()
Preview data with Pandas head function:
head()
function.preview = df.head()
Displaying top rows of a specific column in Pandas:
head()
method.top_column_values = df['Column_Name'].head()
Show top rows after sorting in Pandas:
head()
.sorted_df = df.sort_values(by='Column_Name') top_sorted_rows = sorted_df.head()
Using head() with large DataFrames in Pandas:
head()
to large DataFrames for a quick overview without overwhelming output.quick_look_large_df = df.head(10)
Quick look at data using Pandas head():
head()
for a brief examination of the DataFrame.quick_look = df.head()