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
How to display all rows from dataframe using Pandas?
Pandas is a powerful data manipulation library in Python that provides a flexible way to handle tabular data through its DataFrame object. By default, Pandas truncates DataFrame display output when there are many rows, showing only a limited number to keep output concise and readable.
Using to_string() Method
The to_string() method displays the complete DataFrame regardless of the number of rows or columns ?
import pandas as pd
# Create sample data
data = {
'Name': ['Sachin Tendulkar', 'Brian Lara', 'Ricky Ponting', 'Jacques Kallis', 'Inzamam-ul-Haq'],
'Country': ['India', 'West Indies', 'Australia', 'South Africa', 'Pakistan'],
'Debut Year': [1989, 1990, 1995, 1995, 1991],
'Retirement Year': [2013, 2007, 2012, 2014, 2007]
}
cricketers_df = pd.DataFrame(data)
print(cricketers_df.to_string())
Name Country Debut Year Retirement Year
0 Sachin Tendulkar India 1989 2013
1 Brian Lara West Indies 1990 2007
2 Ricky Ponting Australia 1995 2012
3 Jacques Kallis South Africa 1995 2014
4 Inzamam-ul-Haq Pakistan 1991 2007
Using to_markdown() Method
The to_markdown() method converts the DataFrame to a Markdown-formatted table that's easy to read and can be used in documentation ?
import pandas as pd
data = {
'Name': ['Sachin Tendulkar', 'Brian Lara', 'Ricky Ponting', 'Jacques Kallis', 'Inzamam-ul-Haq'],
'Country': ['India', 'West Indies', 'Australia', 'South Africa', 'Pakistan'],
'Debut Year': [1989, 1990, 1995, 1995, 1991],
'Retirement Year': [2013, 2007, 2012, 2014, 2007]
}
cricketers_df = pd.DataFrame(data)
print(cricketers_df.to_markdown())
| | Name | Country | Debut Year | Retirement Year | |---:|:-----------------|:-------------|-------------:|------------------:| | 0 | Sachin Tendulkar | India | 1989 | 2013 | | 1 | Brian Lara | West Indies | 1990 | 2007 | | 2 | Ricky Ponting | Australia | 1995 | 2012 | | 3 | Jacques Kallis | South Africa | 1995 | 2014 | | 4 | Inzamam-ul-Haq | Pakistan | 1991 | 2007 |
Using option_context() for Temporary Settings
The option_context() function temporarily modifies display options within a specific context. Setting display.max_rows to None shows all rows ?
import pandas as pd
# Create larger dataset
data = {
'Fruit': ['Apple', 'Orange', 'Banana', 'Grapes', 'Pineapple', 'Strawberry', 'Watermelon', 'Kiwi'],
'Color': ['Red', 'Orange', 'Yellow', 'Green', 'Brown', 'Red', 'Green', 'Brown'],
'Weight (oz)': [4.0, 6.0, 5.0, 3.0, 16.0, 1.0, 128.0, 3.0],
'Price ($)': [0.50, 0.40, 0.20, 0.30, 1.50, 0.10, 2.00, 0.30]
}
fruits_df = pd.DataFrame(data)
with pd.option_context('display.max_rows', None):
print(fruits_df)
Fruit Color Weight (oz) Price ($)
0 Apple Red 4.0 0.50
1 Orange Orange 6.0 0.40
2 Banana Yellow 5.0 0.20
3 Grapes Green 3.0 0.30
4 Pineapple Brown 16.0 1.50
5 Strawberry Red 1.0 0.10
6 Watermelon Green 128.0 2.00
7 Kiwi Brown 3.0 0.30
Using set_option() for Permanent Settings
The set_option() method permanently changes display settings until modified again ?
import pandas as pd
data = {
'Fruit': ['Apple', 'Orange', 'Banana', 'Grapes', 'Pineapple', 'Strawberry'],
'Color': ['Red', 'Orange', 'Yellow', 'Green', 'Brown', 'Red'],
'Weight (oz)': [4.0, 6.0, 5.0, 3.0, 16.0, 1.0],
'Price ($)': [0.50, 0.40, 0.20, 0.30, 1.50, 0.10]
}
fruits_df = pd.DataFrame(data)
# Set permanent option to display all rows
pd.set_option('display.max_rows', None)
print(fruits_df)
# Reset to default (optional)
pd.reset_option('display.max_rows')
Fruit Color Weight (oz) Price ($)
0 Apple Red 4.0 0.50
1 Orange Orange 6.0 0.40
2 Banana Yellow 5.0 0.20
3 Grapes Green 3.0 0.30
4 Pineapple Brown 16.0 1.50
5 Strawberry Red 1.0 0.10
Comparison of Methods
| Method | Scope | Output Format | Best For |
|---|---|---|---|
to_string() |
Single operation | Plain text | Console display |
to_markdown() |
Single operation | Markdown table | Documentation |
option_context() |
Temporary | Default format | Context-specific display |
set_option() |
Permanent | Default format | Global settings change |
Conclusion
Use to_string() for quick console output, to_markdown() for documentation, option_context() for temporary changes, and set_option() for permanent display modifications. Choose the method based on whether you need temporary or permanent changes to display settings.
