Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pretty-print an entire Pandas Series / DataFrame

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.

like image 720
Dun Peal Avatar asked Oct 01 '13 19:10

Dun Peal


People also ask

How do you pretty print a DataFrame in Python?

You can pretty print pandas dataframe using pd. set_option('display. max_columns', None) statement. Usecase: Your dataframe may contain many columns and when you print it normally, you'll only see few columns.

How do you show all data frames?

Method 2: Using set_option() display. max_rows represents the maximum number of rows that pandas will display while displaying a data frame. The default value of max_rows is 10. If set to 'None' then it means all rows of the data frame.


1 Answers

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

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 options to their previous values.

If you are working on jupyter-notebook, using display(df) instead of print(df) will use jupyter rich display logic (like so).

like image 200
tsvikas Avatar answered Sep 20 '22 18:09

tsvikas