pandas
data science
python
data analysis
data visualization

Pretty-print an entire Pandas Series / DataFrame

ML System Design practice on Codemia

Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.

Practice ML system design

In the world of data manipulation and analysis, Pandas is a go-to library for Python enthusiasts. It provides an intuitive and efficient abstraction for working with structured data, offering data structures like Series and DataFrame for storing and manipulating data. An essential part of data analysis is the presentation of data, and Pandas offers several options for "pretty-printing" this data, making it human-readable and easily interpretable. This article will explore how to pretty-print Pandas Series and DataFrames, providing technical insights and examples.

Pretty-Printing Pandas Series and DataFrames

Pandas, by default, provides a readable output for both Series and DataFrames—but there are scenarios where default isn't enough, and you may need more refined control over how your data is displayed. Let's explore some techniques and options available in Pandas for pretty-printing your data structures.

Displaying Pandas Series

A Pandas Series is a one-dimensional labeled array. By default, Pandas pretty-prints Series, providing both the values and the index. Here’s an illustrative example:

python
1import pandas as pd
2
3# Creating a simple Pandas Series
4s = pd.Series([1, 2, 3, 4, 5], index=['a', 'b', 'c', 'd', 'e'])
5print(s)

Output:

 
1a    1
2b    2
3c    3
4d    4
5e    5
6dtype: int64

Options for Pretty-Printing Series

Head and Tail Methods

For larger Series, it might be useful to print only the beginning or end of the Series:

python
print(s.head(3))  # Print the first 3 elements
print(s.tail(2))  # Print the last 2 elements

Formatting Output

For numerical data, formatting output can be crucial:

python
s = pd.Series([3.14159265, 2.7182818284, 1.6180339887])
pd.options.display.float_format = '{:.3f}'.format
print(s)

Output:

 
10    3.142
21    2.718
32    1.618
4dtype: float64

Displaying Pandas DataFrames

DataFrames are essentially two-dimensional tables. They are displayed in a tabular form with index and columns. Here’s a minimal example:

python
1import numpy as np
2
3data = {
4    'A': np.random.rand(4),
5    'B': np.random.rand(4),
6    'C': np.random.rand(4)
7}
8df = pd.DataFrame(data)
9print(df)

Output:

 
1          A         B         C
20  0.693618  0.599423  0.627235
31  0.978199  0.256585  0.597473
42  0.732396  0.190138  0.846335
53  0.063705  0.142206  0.072057

Options for Pretty-Printing DataFrames

Controlling Maximum Rows and Columns

By default, Pandas decides how many rows and columns to display based on the terminal size. You can override these settings:

python
pd.set_option('display.max_rows', 10)
pd.set_option('display.max_columns', 5)

Column Width

Adjusting column width ensures readability when you have columns with long names or content:

python
pd.set_option('display.max_colwidth', 50)

Styler Class for DataFrames

The Styler class provides more advanced options like adding data bars, background gradients, and more:

python
styled_df = df.style.highlight_max(axis=0)
styled_df

The above will highlight the maximum value of each column in the DataFrame.

Exporting for Pretty-Printing

Sometimes, exporting to a different format like HTML or LaTeX is preferable for including in reports:

python
1# Export to HTML
2html_representation = df.to_html()
3print(html_representation)
4
5# Export to LaTeX
6latex_representation = df.to_latex()
7print(latex_representation)

Summary Table

Below is a summary of key options for pretty-printing Pandas Series and DataFrames:

OptionDescriptionExample Usage
head(n)Display the first n rows of a Series/DataFramedf.head(5)
tail(n)Display the last n rows of a Series/DataFrames.tail(3)
set_optionSet global display options (e.g., max rows/columns)pd.set_option('display.max_rows', 10)
float_formatControl float formattingpd.options.display.float_format = '{:.3f}'.format
max_colwidthSet max width for columns to ensure readabilitypd.set_option('display.max_colwidth', 50)
styleCustomize DataFrame display with Styler classdf.style.highlight_max(axis=0)
to_html()Export DataFrame to HTML for web-based reportsdf.to_html()
to_latex()Convert DataFrame to LaTeX format for academic reportsdf.to_latex()

Conclusion

Pretty-printing of Pandas Series and DataFrames is a vital part of data analysis, allowing for better readability and comprehension of the data being handled. By leveraging Pandas' built-in options and functionality, data presentation can be fine-tuned to meet specific needs. Whether it is setting display options, formatting numerical data, or exporting DataFrames to other formats, Pandas has robust capabilities that aid both developers and data analysts in their tasks. As you explore Pandas further, consider these pretty-printing methods to enhance your data manipulation and visualization skills.


Related reading
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

ML System Design practice on Codemia

Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.

Practice ML system design

All Rights Reserved.