Python Pandas: How to display full Dataframe i.e. print all rows & columns without truncation

In this tutorial, we will discuss the different methods to display full Dataframe i.e. print all rows & columns without truncation. So, get into this page and learn completely about Python Pandas like How to display a full Dataframe i.e. print all rows & columns without truncation.

Display Full Contents of a Dataframe

Pandas implement an operating system to customize the behavior & display similar stuff. By applying this benefits module we can configure the display to show the complete dataframe rather than a truncated one. A function set_option()is provided in pandas to set this kind of option,

pandas.set_option(pat, value)

It sets the value of the defined option. Let’s use this to display the full contents of a dataframe.

Also Check:

How to print an entire Pandas DataFrame in Python?

When we use a print large number of a dataset then it truncates. In this article, we are going to see how to print the entire pandas Dataframe or Series without Truncation.

The complete data frame is not printed when the length exceeds.

import numpy as np
from sklearn.datasets import load_iris
import pandas as pd
  
# Loading irirs dataset
data = load_iris()
df = pd.DataFrame(data.data,columns = data.feature_names)
print(df)

Output:

How-to-print-an-entire-Pandas-DataFrame-in-Python.png

By default our complete contents of out dataframe are not printed, output got truncated. It printed only 10 rows all the remaining data is truncated. Now, what if we want to print the full dataframe without any truncation.

Four Methods to Print the entire pandas Dataframe

  1. Use to_string() Method
  2. Use pd.option_context() Method
  3. Use pd.set_options() Method
  4. Use pd.to_markdown() Method

1. Using to_string()

This is a very simple method. That is why it is not used for large files because it converts the entire data frame into a string object. But this works very well for data frames for size in the order of thousands.

import numpy as np
from sklearn.datasets import load_iris
import pandas as pd
  
data = load_iris()
df = pd.DataFrame(data.data,
                  columns = data.feature_names)
  
# Convert the whole dataframe as a string and display
print(df.to_string())

Output:

How-to-display-full-Dataframe-i.e.-print-all-rows-columns-without-truncation_output.pn

So in the above example, you have seen it printed all columns without any truncation.

2. Using pd.option_context()

option_context() and set_option() both methods are identical but there is only one difference that is one changes the settings and the other do it only within the context manager scope.

import numpy as np
from sklearn.datasets import load_iris
import pandas as pd
  
data = load_iris()
df = pd.DataFrame(data.data, 
                  columns = data.feature_names)
  
with pd.option_context('display.max_rows', None,'display.max_columns', None,
    'display.precision', 3,
                       ):
print(df)

Output:

How-to-display-full-Dataframe-i.e.-print-all-rows-columns-without-truncation_output.pn

In the above example, we are used ‘display.max_rows‘ but by default its value is 10 & if the dataframe has more rows it will truncate. So it will not be truncated we used None so all the rows are displayed.

3. Using pd.set_option()

This method is similar to pd.option_context() method and takes the same parameters. pd.reset_option(‘all’) used to reset all the changes.

import numpy as np
from sklearn.datasets import load_iris
import pandas as pd
  
data = load_iris()
df = pd.DataFrame(data.data,
                  columns = data.feature_names)
  
# Permanently changes the pandas settings
pd.set_option('display.max_rows', None)
pd.set_option('display.max_columns', None)
pd.set_option('display.width', None)
pd.set_option('display.max_colwidth', -1)
  
# All dataframes hereafter reflect these changes.
print(df)
  
print('**RESET_OPTIONS**')
  
# Resets the options
pd.reset_option('all')
print(df)

Output:

How-to-display-full-Dataframe-i.e.-print-all-rows-columns-without-truncation_output.pn

**RESET_OPTIONS**

: boolean
use_inf_as_null had been deprecated and will be removed in a future
version. Use `use_inf_as_na` instead.

How-to-print-an-entire-Pandas-DataFrame-in-Python.png

4. Using to_markdown()

This method is similar to the to_string() method as it also converts the data frame to a string object and also adds styling & formatting to it.

import numpy as np
from sklearn.datasets import load_iris
import pandas as pd
  
data = load_iris()
df = pd.DataFrame(data.data,
                  columns=data.feature_names)
  
# Converts the dataframe into str object with fromatting
print(df.to_markdown())

Output:
How-to-display-full-Dataframe-i.e.-print-all-rows-columns-without-truncation_output.pn

Want to expert in the python programming language? Exploring Python Data Analysis using Pandas tutorial changes your knowledge from basic to advance level in python concepts.

Read more Articles on Python Data Analysis Using Padas