pandasで作成されたテーブル内の文字フォントをでかくする

その買うを、もっとハッピーに。|ハピタス

pandasのdataframeがちっこ過ぎて、おっさんで老眼の人間には米粒に書いてある文字を読むのに等しいので、テーブルの文字をでかくする方法をあれこれネットで探索してみた。以下がその結果だ。

スポンサーリンク

dataframeの文字拡大方法

データをダウンロードする

デフォルトだと、データフレームが狭く、テーブル内の文字があまりにも小さ過ぎて全く見えない状態であることが分かる。先ずは、データを以下のサイトからダウンロードしてくる。

%download https://raw.githubusercontent.com/pandas-dev/pandas/master/pandas/tests/data/iris.csv
Downloaded 'iris.csv'.
import pandas as pd
%matplotlib inline
from IPython.display import HTML

iris=pd.read_csv("iris.csv")

styles = [
    dict(selector="th", props=[("font-size", "100%"),
                               ("text-align", "center")])
]
html = (iris.head().style.set_table_styles(styles))
html
SepalLength SepalWidth PetalLength PetalWidth Name
0 5.1 3.5 1.4 0.2 Iris-setosa
1 4.9 3 1.4 0.2 Iris-setosa
2 4.7 3.2 1.3 0.2 Iris-setosa
3 4.6 3.1 1.5 0.2 Iris-setosa
4 5 3.6 1.4 0.2 Iris-setosa

上のように、とにかくテーブルが小さ過ぎて、テーブル内の文字は解読不能である。

CSSで数字を拡大する

from IPython.core.display import display, HTML

css = """
table.dataframe {
    font-size: 1.5em;
}
"""
display(HTML('<style>{}</style>'.format(css)))
iris.head()
SepalLength SepalWidth PetalLength PetalWidth Name
0 5.1 3.5 1.4 0.2 Iris-setosa
1 4.9 3.0 1.4 0.2 Iris-setosa
2 4.7 3.2 1.3 0.2 Iris-setosa
3 4.6 3.1 1.5 0.2 Iris-setosa
4 5.0 3.6 1.4 0.2 Iris-setosa

以上のように、CSSを使えば非常に簡単にテーブル内の数字や文字のフォントを変えられ、老眼親父でも見やすいテーブルを作成することができる。

スポンサーリンク
スポンサーリンク