Open In App

How to display all rows from dataframe using Pandas

Last Updated : 30 Nov, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

In this article, we will discuss how to display all rows from dataframe using Pandas in Python. We will discuss different methods by which we can display all rows from dataframe using Pandas.

Create a Diabetes Dataframe and Display all Rows from Dataframe

When we try to print a large data frame that exceeds the predefined number of rows and columns to display, the result will be truncated. In the below example, we can see the total number of rows is 442, but it displays only TEN rows. This is due to by default setting in the pandas library being TEN rows only (the default number of rows may change depending on the system). Now we will see how to display all rows from the data frame using pandas in Python.

Python3




# importing numpy library
import pandas as pd
 
# importing diabetes dataset from sklearn
from sklearn.datasets import load_diabetes
 
# Loading diabetes dataset
data = load_diabetes()
 
# storing as data frame
dataframe = pd.DataFrame(data.data, columns=data.feature_names)
 
# printing data frame
print(dataframe)


Output:

Display all Rows from Dataframe using Pandas

Below are the methods by which we can display all rows from dataframe using Pandas:

  • Using to_string()
  • Using set_option()
  • Using to_markdown()
  • Using option_context()

Display all Rows from Dataframe Using to_string()

In this example, we are using to_string() function to display all rows from dataframe using Pandas. Here, the code uses pandas to create a DataFrame from the Iris dataset, which is loaded from scikit-learn. It then converts the entire DataFrame to a string representation and prints it, displaying all rows and columns of the dataset.

Python3




# Display all rows from data frame using pandas
# importing numpy library
import pandas as pd
 
# importing iris dataset from sklearn
from sklearn.datasets import load_iris
 
# Loading iris dataset
data = load_iris()
 
# storing as data frame
dataframe = pd.DataFrame(data.data, columns=data.feature_names)
 
# Convert entire data frame as string and print
print(dataframe.to_string())


Output

Here, In the output, you can see it display all rows from the data frame. But this method is not advisable for large data frame as this convert whole data frame into a string so there might be any memory error.

Show All Rows of a Pandas DataFrame using set_option()

In this example, we are using set_option() function to display all rows from dataframe using Pandas. Here, the code sets the pandas display option to show all rows (display.max_rows is set to None) and then creates a DataFrame from the Iris dataset using scikit-learn. Finally, it prints the entire DataFrame, displaying all rows and columns of the Iris dataset.

Python3




# Display all rows from data frame using pandas
# importing numpy library
import pandas as pd
 
# importing iris dataset from sklearn
from sklearn.datasets import load_iris
 
# Loading iris dataset
data = load_iris()
 
pd.set_option('display.max_rows', None)
 
# storing the dataset as data frame
dataframe = pd.DataFrame(data.data, columns=data.feature_names)
 
# printing data frame
print(dataframe)


Output

Display all Rows from Dataframe Using to_markdown()

In this example, we are using to_markdown() function to display all rows from dataframe using Pandas. Here, the code creates a DataFrame from the Iris dataset using pandas and then converts the entire DataFrame to a markdown format, displaying it when printed. This provides a formatted representation of the dataset in Markdown syntax.

Python3




# importing numpy library
import pandas as pd
 
# importing iris dataset from sklearn
from sklearn.datasets import load_iris
 
# Loading iris dataset
data = load_iris()
 
# storing the dataset as data frame
dataframe = pd.DataFrame(data.data, columns=data.feature_names)
 
# Convert entire data frame as markdown and print
print(dataframe.to_markdown())


Output

Show All Rows of a Pandas DataFrame using option_context()

This method is identical to set_option() method. Both methods are the same only difference is set_option() change the settings permanently and option_context() former do it only within its scope. This method also uses display.max_rows as parameters and we make it equal to None to display all rows of the data frame. So, after setting the value of the display.max_rows to None and passing it to option_context we will be able to see all rows from the data frame. Its syntax is the same as set_option() method.

Syntax :

with pandas.option_context(‘display.max_rows’, None,): print(dataframe)

Example: In this example, we are using option_context(). Here, the code temporarily sets the pandas display option to show all rows (display.max_rows is set to None) within the scope of a with statement, allowing the entire DataFrame created from the Iris dataset to be printed with all rows displayed. Once outside the with block, the display option reverts to its previous state.

Python3




# Display all rows from data frame using pandas
# importing numpy library
import pandas as pd
 
# importing iris dataset from sklearn
from sklearn.datasets import load_iris
 
# Loading iris dataset
data = load_iris()
 
# storing the dataset as data frame
dataframe = pd.DataFrame(data.data, columns=data.feature_names)
 
# The scope of these changes
# are local with systems to with statement.
with pd.option_context('display.max_rows', None,):
    print(dataframe)


Output



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads