Skip to content

Handling pandas in the output¤

Pandas is a widely used data manipulation library in the data science community. One common use case is to display the results of a data analysis in a Pandas DataFrame.

By default, the Pandas DataFrame output will appear unformatted in the documentation. For example, take a look at the sample below which displays the head of a dataframe.

import pandas as pd

df = pd.util.testing.makeDataFrame()
df.head()
A B C D
rLc2n1qWZG -0.965469 -1.142862 -1.427434 0.301794
G54tM9ysyl -1.074022 -1.069067 -0.790344 -0.204448
LjjfJhQ1tb -1.267221 -0.208394 1.101776 -0.705727
iLEBa0Mo1o -0.725662 -0.331378 0.055890 -0.073847
I9gxbActLA 0.229448 0.121855 -0.534225 -0.990810

Material styled table¤

A simple solution to enhance the appearance of the Pandas DataFrame table is to use the DataFrame.style attribute while displaying the output.

df.head().style
  A B C D
rLc2n1qWZG -0.965469 -1.142862 -1.427434 0.301794
G54tM9ysyl -1.074022 -1.069067 -0.790344 -0.204448
LjjfJhQ1tb -1.267221 -0.208394 1.101776 -0.705727
iLEBa0Mo1o -0.725662 -0.331378 0.055890 -0.073847
I9gxbActLA 0.229448 0.121855 -0.534225 -0.990810