Pretty-print an entire Pandas Series / DataFrame

2019-01-01 09:41发布

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 rest missing.

Is there a builtin way to pretty-print the entire Series / DataFrame? Ideally, it would support proper alignment, perhaps borders between columns, and maybe even color-coding for the different columns.

9条回答
孤独寂梦人
2楼-- · 2019-01-01 10:29

You can also use the option_context, with one or more options:

with pd.option_context('display.max_rows', None, 'display.max_columns', None):
    print(df)

This will automatically return the options to their default values.

If you are working on jupyter-notebook, using display instead of print will use jupyter rich display logic.

查看更多
旧时光的记忆
3楼-- · 2019-01-01 10:29

Try this

pd.set_option('display.height',1000)
pd.set_option('display.max_rows',500)
pd.set_option('display.max_columns',500)
pd.set_option('display.width',1000)
查看更多
还给你的自由
4楼-- · 2019-01-01 10:31

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_options() -> None:
    pd.options.display.max_columns = 1000
    pd.options.display.max_rows = 1000
    pd.options.display.max_colwidth = 199
    pd.options.display.width = None
    # pd.options.display.precision = 2  # set as needed

set_pandas_options()

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

查看更多
登录 后发表回答