Open In App

How to print an entire Pandas DataFrame in Python?

Last Updated : 26 Jun, 2024
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

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. There are 4 methods to Print the entire Dataframe.

Example

# Convert the whole dataframe as a string and display
display(df.to_string())

By default, the complete data frame is not printed if the length exceeds the default length, the output is truncated as shown below: 

Python
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)
display(df)

Output:

While this method is simplest of all, it is not advisable for very huge datasets (in order of millions) because it converts the entire data frame into a string object but works very well for data frames for size in the order of thousands.

Example: In this example, we are using the load_iris function from scikit-learn to load the Iris dataset, then creates a pandas DataFrame (df) containing the dataset features, and finally, converts the entire DataFrame to a string representation using to_string() and displays it.

Python
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
display(df.to_string())

Output:

Pandas allow changing settings via the option_context() method and set_option() methods. Both the methods are identical with one difference that later one changes the settings permanently and the former do it only within the context manager scope.

Syntax : pandas.option_context(*args)

Example: In this example, we are using the Iris dataset from scikit-learn, creates a pandas DataFrame (df) with specified formatting options, and prints the DataFrame within a temporary context where display settings, such as maximum rows, columns, and precision, are modified for local scope only.

Python
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)

# The scope of these changes made to
# pandas settings are local to with statement.
with pd.option_context('display.max_rows', None,
                       'display.max_columns', None,
                       'display.precision', 3,
                       ):
    print(df)

Output:

Pandas Print Dataframe using pd.set_option()

This method is similar to pd.option_context() method and takes the same parameters as discussed for method 2, but unlike pd.option_context() its scope and effect is on the entire script i.e all the data frames settings are changed permanently 

To explicitly reset the value use pd.reset_option(‘all’) method has to be used to revert the changes.

Syntax : pandas.set_option(pat, value)

Example: This code modifies global pandas display options to show all rows and columns with unlimited width and precision for the given DataFrame (df). It then resets the options to their default values and displays the DataFrame again, illustrating the restoration of default settings.

Python
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.
display(df)

print('**RESET_OPTIONS**')

# Resets the options
pd.reset_option('all')
display(df)

Output:

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.

Syntax : DataFrame.to_markdown(buf=None, mode=’wt’, index=True,, **kwargs)

Example: This code uses the Iris dataset from scikit-learn to create a pandas DataFrame (df), and then it prints a formatted Markdown representation of the DataFrame using the to_markdown() method.

Python
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 formatting
print(df.to_markdown())

Output:



Next Article

Similar Reads

three90RightbarBannerImg