Enough to print the entire Pandas / DataFrame series

I work a lot with Series and DataFrames on the terminal. The default __repr__ for the series returns a reduced selection with some __repr__ and tail values, but the rest are missing.

Is there a built-in way to beautifully print the entire series / DataFrame? Ideally, it will maintain proper alignment, perhaps the border between columns, and maybe even color coding for different columns.

+495
python pandas dataframe
Oct 01 '13 at 19:46
source share
10 answers

You can also use option_context with one or more parameters:

 with pd.option_context('display.max_rows', None, 'display.max_columns', None): # more options can be specified also print(df) 

This will automatically return the parameters to their previous values.

If you are working with jupyter-notebook, using display(df) instead of print(df) will use jupyter's extended display logic (e.g. ).

+667
Jun 07 '15 at 9:22
source share
— -

No need to hack into settings. There is an easy way:

 print(df.to_string()) 
+484
Oct 07 '16 at 18:45
source share

Of course, if it's a lot, make such a function. You can even configure it to load every time IPython starts: https://ipython.org/ipython-doc/1/config/overview.html

 def print_full(x): pd.set_option('display.max_rows', len(x)) print(x) pd.reset_option('display.max_rows') 

As far as coloring, too complex with colors seems counterproductive, but I agree with something like bootstrap .table-striped . You can always create a problem to offer this feature.

+160
Oct 01 '13 at
source share

After importing pandas, as an alternative to using the context manager, set these parameters to display entire frames of data:

 pd.set_option('display.max_columns', None) # or 1000 pd.set_option('display.max_rows', None) # or 1000 pd.set_option('display.max_colwidth', -1) # or 199 

See the full list of useful options:

 pd.describe_option('display') 
+82
May 20 '16 at 13:18
source share

Use the batch package:

 pip install tabulate 

And consider the following usage example:

 import pandas as pd from io import StringIO from tabulate import tabulate c = """Chromosome Start End chr1 3 6 chr1 5 7 chr1 8 9""" df = pd.read_table(StringIO(c), sep="\s+", header=0) print(tabulate(df, headers='keys', tablefmt='psql')) +----+--------------+---------+-------+ | | Chromosome | Start | End | |----+--------------+---------+-------| | 0 | chr1 | 3 | 6 | | 1 | chr1 | 5 | 7 | | 2 | chr1 | 8 | 9 | +----+--------------+---------+-------+ 
+37
Apr 09 '18 at 19:02
source share

If you are using an Ipython Notebook (Jupyter). You can use html

 from IPython.core.display import HTML display(HTML(df.to_html())) 
+14
Mar 20 '18 at 1:56
source share

This answer is a variation of the previous answer from lucidyan . This makes the code more readable, avoiding the use of set_option .

After importing pandas, as an alternative to using the context manager, set these parameters to display large data frames:

 def set_pandas_display_options() -> None: 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 that, you can use display(df) or just df when using a laptop, otherwise print(df) .

+10
Sep 20 '18 at 20:23
source share

try it

 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) 
+8
Dec 07 '17 at 2:21 on
source share

You can achieve this using the method below. just convey the general no. columns represented in the DataFrame as an argument

'display.max_columns'

For example:

 df= DataFrame(..) with pd.option_context('display.max_rows', None, 'display.max_columns', df.shape[1]): print(df) 
+1
Jun 15 '18 at 5:59
source share

Try using the display () function. This will automatically use horizontal and vertical scrollbars, and you can easily display different datasets instead of using print ().

 display(dataframe) 

display () also supports proper alignment.

However, if you want to make the data set more beautiful, you can check pd.option_context() . It has many options to clearly show the data frame.

Note. I use Jupyter laptops.

-four
Aug 26 '19 at 10:44 on
source share



All Articles