How to print an entire Pandas DataFrame in Python?
Published on Aug. 22, 2023, 12:15 p.m.
There are several ways to print an entire Pandas DataFrame in a “pretty” format. Some of the most commonly used methods are:
- Using the
to_string()
method: This method converts a DataFrame to a string representation, allowing you to print the entire DataFrame. For example:
print(df.to_string())
- Using
pd.set_option()
: This method can be used to set display options for Pandas objects, such as the maximum number of rows and columns to display. Setting these options toNone
will allow you to display the entire DataFrame. For example:
import pandas as pd
pd.set_option("display.max_rows", None, "display.max_columns", None)
print(df)
- Using
pd.option_context()
: This method also allows you to temporarily set display options, but only for the duration of a specific code block. For example:
import pandas as pd
with pd.option_context("display.max_rows", None, "display.max_columns", None):
print(df)
- Using
IPython.display
functions: If you’re using IPython or Jupyter notebooks, you can use thedisplay()
function to display a DataFrame in a more readable format. For example:
import IPython.display as ipd
ipd.display(df)
Note that in all cases, substituting df
with the name of your DataFrame.