[PYTHON] Changed the default style (CSS) of pandas data frame output by display in Google Colab

Overview

This is a method to change the default appearance (style / CSS) of the data frame pandas.DataFrame output via the IPython.display module in GoogleColab. 2020-08-12_17h20_05.png

In addition, we have confirmed the operation with GoogelChrome.

Change data frame style

If you execute the following in the code cell of GoogleColab.

python


import pandas as pd
df = pd.DataFrame([[1, 2, 3],[4, 5, 6],[7, 8, 9]],
                  columns=['Osaka','Tokyo','Aichi'])
display(df)

A data frame ** formatted using HTML ** table elements is output as follows:
2020-08-12_17h49_15.png

If you want to customize the style (CSS) of this table, insert code like this: After that, the CSS specified in the code will be applied to the ** data frame (HTML table element) output via ʻIPython.display (...)` **.

Change the CSS applied to the output of the data frame


import IPython

def set_css_in_cell_output():
  display(IPython.display.HTML('''<style>
  table.dataframe td, table.dataframe th{
    border:1px solid #dadada;
  }
  table.dataframe th{
    font-family: 'Roboto','Noto',sans-serif;
    background: #eeeeee;
    font-weight: bold;
  }
  table.dataframe td{
    background: #ffffff;
  }
  table.dataframe tbody tr:hover th{
    color: #ffffff;
    background-color:#4169e1;
  }
  table.dataframe tr:hover td{
    background-color: #d7eeff;
  }
  </style>'''))

get_ipython().events.register('pre_run_cell', set_css_in_cell_output)

From now on, the dataframe will be output as follows (all tables are now bordered and the header font and mouseover color will change):
2020-08-12_17h50_00.png

Conditional formatting

It seems to use DataFrame.style if you want to apply ** conditional formatting **, such as changing the color according to the number in the cell.

Reference material

Recommended Posts

Changed the default style (CSS) of pandas data frame output by display in Google Colab
Find the index of items that match the conditions in the pandas data frame / series
Gradually display the output of the command executed by subprocess.Popen
Comparison of data frame handling in Python (pandas), R, Pig
Read the standard output of a subprocess line by line in Python
[Android] Display images on the web in the info Window of Google Map
[Google Colab] I want to display multiple images side by side in tiles
Pandas of the beginner, by the beginner, for the beginner [Python]
[IOS] Change the display time for each frame of GIF animation in Pythonista3.
Change the data frame of pandas purchase data (id x product) to a dictionary
Basic data frame operations written by beginners in a week of learning Python
Try to display the railway data of national land numerical information in 3D
Output the number of CPU cores in Python
Read the output of subprocess.Popen in real time
The story of reading HSPICE data in Python
How to display videos inline in Google Colab
Output in the form of a python array
Search by the value of the instance in the list
Use Pandas to write only the specified lines of the data frame to an excel file
Find out the maximum number of characters in multi-line text stored in a data frame