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.
In addition, we have confirmed the operation with GoogelChrome.
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:
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):
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.
Recommended Posts