Pretty-print an entire Pandas Series / DataFrame

后端 未结 12 2388
再見小時候
再見小時候 2020-11-22 03:26

I work with Series and DataFrames on the terminal a lot. The default __repr__ for a Series returns a reduced sample, with some head and tail values, but the res

12条回答
  •  無奈伤痛
    2020-11-22 04:04

    Using pd.options.display

    This answer is a variation of the prior answer by lucidyan. It makes the code more readable by avoiding the use of set_option.

    After importing pandas, as an alternative to using the context manager, set such options for displaying large dataframes:

    def set_pandas_display_options() -> None:
        """Set pandas display options."""
        # Ref: https://stackoverflow.com/a/52432757/
        display = pd.options.display
    
        display.max_columns = 1000
        display.max_rows = 1000
        display.max_colwidth = 199
        display.width = None
        # display.precision = 2  # set as needed
    
    set_pandas_display_options()
    

    After this, you can use either display(df) or just df if using a notebook, otherwise print(df).

    Using to_string

    Pandas 0.25.3 does have DataFrame.to_string and Series.to_string methods which accept formatting options.

    Using to_markdown

    If what you need is markdown output, Pandas 1.0.0 has DataFrame.to_markdown and Series.to_markdown methods.

    Using to_html

    If what you need is HTML output, Pandas 0.25.3 does have a DataFrame.to_html method but not a Series.to_html. Note that a Series can be converted to a DataFrame.

提交回复
热议问题