How to display all rows from dataframe using Pandas
Last Updated :
30 Nov, 2023
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
import pandas as pd
from sklearn.datasets import load_diabetes
data = load_diabetes()
dataframe = pd.DataFrame(data.data, columns = data.feature_names)
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
import pandas as pd
from sklearn.datasets import load_iris
data = load_iris()
dataframe = pd.DataFrame(data.data, columns = data.feature_names)
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
import pandas as pd
from sklearn.datasets import load_iris
data = load_iris()
pd.set_option( 'display.max_rows' , None )
dataframe = pd.DataFrame(data.data, columns = data.feature_names)
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
import pandas as pd
from sklearn.datasets import load_iris
data = load_iris()
dataframe = pd.DataFrame(data.data, columns = data.feature_names)
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
import pandas as pd
from sklearn.datasets import load_iris
data = load_iris()
dataframe = pd.DataFrame(data.data, columns = data.feature_names)
with pd.option_context( 'display.max_rows' , None ,):
print (dataframe)
|
Output
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...