Explain Codes LogoExplain Codes Logo

Pandas: Setting the number of maximum rows

python
dataframe
display-options
pandas
Nikita BarsukovbyNikita Barsukov·Dec 16, 2024
TLDR

To control the number of rows displayed in a DataFrame, utilize the command pd.set_option('display.max_rows', num), where num is a specified max number of rows or use None for unlimited rows.

pd.set_option('display.max_rows', 10) # Now it shows only 10 rows before giving up.

Quick modifications: Adjusting display settings

Pandas provides a convenient way to temporarily adjust display options within a specified context using pd.option_context. The changes apply only to the specific block of code.

with pd.option_context('display.max_rows', None): print(df) # Long DataFrame? Challenge accepted.

Beautifying your DataFrame: width and precision

Set the specific display width and decimal precision for your DataFrame. Customize to fit your preference with 'display.width' and 'display.precision'.

pd.set_option('display.width', 150) # Let's stretch this DataFrame out. pd.set_option('display.precision', 2) # In the case of doubt, round it to the nearest cent.

Tailoring your environment: contextual adaptations

Environment-specific display options

Different environments will have varied display behaviors. For example, Jupyter notebooks may render DataFrames using df.head(500) instead of set_option.

Back to defaults

It's crucial to remember how to revert to default settings when needed. Use pd.reset_option to revert a single option and pd.reset_option('all') for all.

Discover options with tab-completion

In iPython environments like Jupyter, you can explore options by typing pd.options. and pressing Tab to see all the settings within your reach.

Going beyond: Exploring additional options

Explore more display options

To look into all available options, use pd.describe_option('display') to discover and tweak your data display.

Jupyter Notebook specifications

Jupyter allows more extensive DataFrame display adjustments like:

  • Converting the DataFrame to HTML with a static display using: HTML(df.to_html(max_rows=10)).
  • Adding custom CSS styles to such output with df.style.set_table_styles.