Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Selected Reading
Python Pandas - Display specific number of rows from a DataFrame
To display specific number of rows from a DataFrame, use the head() function. Set the parameter to be the number of row records to be fetched. For example, for 10 rows, mention ?
dataFrame.head(10)
Basic Syntax
The head() method displays the first n rows of a DataFrame ?
DataFrame.head(n)
Where n is the number of rows to display. If not specified, it defaults to 5.
Creating Sample DataFrame
Let's create a sample DataFrame to demonstrate ?
import pandas as pd
# Create sample data
data = {
'Car': ['Audi', 'Porsche', 'RollsRoyce', 'BMW', 'Mercedes',
'Lamborghini', 'Audi', 'Mercedes', 'Lamborghini'],
'Place': ['Bangalore', 'Mumbai', 'Pune', 'Delhi', 'Hyderabad',
'Chandigarh', 'Mumbai', 'Pune', 'Delhi'],
'UnitsSold': [80, 110, 100, 95, 80, 80, 100, 120, 100]
}
dataFrame = pd.DataFrame(data)
print("Complete DataFrame:")
print(dataFrame)
Complete DataFrame:
Car Place UnitsSold
0 Audi Bangalore 80
1 Porsche Mumbai 110
2 RollsRoyce Pune 100
3 BMW Delhi 95
4 Mercedes Hyderabad 80
5 Lamborghini Chandigarh 80
6 Audi Mumbai 100
7 Mercedes Pune 120
8 Lamborghini Delhi 100
Displaying Specific Number of Rows
First 5 Rows (Default)
By default, head() returns the first 5 rows ?
import pandas as pd
# Create sample data
data = {
'Car': ['Audi', 'Porsche', 'RollsRoyce', 'BMW', 'Mercedes',
'Lamborghini', 'Audi', 'Mercedes', 'Lamborghini'],
'Place': ['Bangalore', 'Mumbai', 'Pune', 'Delhi', 'Hyderabad',
'Chandigarh', 'Mumbai', 'Pune', 'Delhi'],
'UnitsSold': [80, 110, 100, 95, 80, 80, 100, 120, 100]
}
dataFrame = pd.DataFrame(data)
print("First 5 rows:")
print(dataFrame.head())
First 5 rows:
Car Place UnitsSold
0 Audi Bangalore 80
1 Porsche Mumbai 110
2 RollsRoyce Pune 100
3 BMW Delhi 95
4 Mercedes Hyderabad 80
Custom Number of Rows
You can specify any number of rows to display ?
import pandas as pd
# Create sample data
data = {
'Car': ['Audi', 'Porsche', 'RollsRoyce', 'BMW', 'Mercedes',
'Lamborghini', 'Audi', 'Mercedes', 'Lamborghini'],
'Place': ['Bangalore', 'Mumbai', 'Pune', 'Delhi', 'Hyderabad',
'Chandigarh', 'Mumbai', 'Pune', 'Delhi'],
'UnitsSold': [80, 110, 100, 95, 80, 80, 100, 120, 100]
}
dataFrame = pd.DataFrame(data)
print("First 3 rows:")
print(dataFrame.head(3))
print("\nFirst 7 rows:")
print(dataFrame.head(7))
First 3 rows:
Car Place UnitsSold
0 Audi Bangalore 80
1 Porsche Mumbai 110
2 RollsRoyce Pune 100
First 7 rows:
Car Place UnitsSold
0 Audi Bangalore 80
1 Porsche Mumbai 110
2 RollsRoyce Pune 100
3 BMW Delhi 95
4 Mercedes Hyderabad 80
5 Lamborghini Chandigarh 80
6 Audi Mumbai 100
Related Methods
For displaying rows from the end of DataFrame, use tail() ?
import pandas as pd
# Create sample data
data = {
'Car': ['Audi', 'Porsche', 'RollsRoyce', 'BMW', 'Mercedes',
'Lamborghini', 'Audi', 'Mercedes', 'Lamborghini'],
'Place': ['Bangalore', 'Mumbai', 'Pune', 'Delhi', 'Hyderabad',
'Chandigarh', 'Mumbai', 'Pune', 'Delhi'],
'UnitsSold': [80, 110, 100, 95, 80, 80, 100, 120, 100]
}
dataFrame = pd.DataFrame(data)
print("Last 3 rows:")
print(dataFrame.tail(3))
Last 3 rows:
Car Place UnitsSold
6 Audi Mumbai 100
7 Mercedes Pune 120
8 Lamborghini Delhi 100
Conclusion
Use head(n) to display the first n rows of a DataFrame. It's useful for quickly inspecting your data without displaying the entire dataset.
Advertisements
