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 | |------------|-----------|-----------|-----------|----------| | vtQ4AqzUmR | -0.366719 | 0.728404 | 0.625120 | 2.480765 | | qiHhTHAHNH | -0.359968 | -0.599540 | 1.594778 | 0.921716 | | LOqytDK7SY | -0.627586 | 0.935781 | -1.592791 | 0.024485 | | WOG0u390DM | 0.105248 | -0.062309 | 0.878165 | 1.072234 | | eOrtbW98MM | -1.400879 | -0.260574 | -0.369980 | 1.760192 |

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 | |------------|-----------|-----------|-----------|----------| | vtQ4AqzUmR | -0.366719 | 0.728404 | 0.625120 | 2.480765 | | qiHhTHAHNH | -0.359968 | -0.599540 | 1.594778 | 0.921716 | | LOqytDK7SY | -0.627586 | 0.935781 | -1.592791 | 0.024485 | | WOG0u390DM | 0.105248 | -0.062309 | 0.878165 | 1.072234 | | eOrtbW98MM | -1.400879 | -0.260574 | -0.369980 | 1.760192 |