LoginSignup
26

More than 5 years have passed since last update.

Jupyterの小技

Last updated at Posted at 2016-06-21

これなに

Python3 のJupyter(IPython notebook)で、以下の方法をご紹介します。

  • pandasの大きいDataFrameを省略せずに表示
  • ジェネレーターの展開
  • Notebookの横幅を広げる
  • DataFrameのインデックスを非表示に

準備

最初に、more-itertools をインストールしてください。

bash
pip install more-itertools

次に、下記を実行してください。または、<ユーザのディレクトリ>/.ipython/profile_default/startup に下記の内容のファイルを置いてください。

jupyter
import IPython.core.getipython
from more_itertools import chunked
ip = IPython.core.getipython.get_ipython()
class ChunkedDataFrame:
    def __init__(self, *args): self.df, self.n, self.m = args
    def _repr_html_(self):
        return ('' if not isinstance(self.df, pd.DataFrame) else
            '\n'.join(self.df.iloc[:self.m, r]._repr_html_()
                      for r in chunked(range(self.df.shape[1]), self.n)))
def C_impl(self, args):
    return ChunkedDataFrame(_, *[int(i) for i in (args+' 12 5').split()[:2]])
def L_impl(self, args):
    print(list(_))
if ip:
    ip.define_magic('C', C_impl)
    ip.define_magic('L', L_impl)

pandasの大きいDataFrameを省略せずに表示

ダミーのDataFrameを表示してみましょう。

jupyter
import numpy as np, pandas as pd
a = pd.DataFrame(np.random.randint(0, 100, (3, 24)))
a
0 1 2 3 4 5 6 7 8 9 ... 14 15 16 17 18 19 20 21 22 23
0 0 30 60 68 53 81 60 10 49 28 ... 49 84 24 92 6 54 52 60 52 71
1 18 86 70 14 10 51 94 10 9 46 ... 90 81 22 65 50 93 28 83 71 22
2 62 58 42 79 83 71 22 44 7 52 ... 54 63 93 14 8 54 55 73 0 26

途中が省略されています。DataFrame表示に続けて、以下のように実行します。

jupyter
%C
0 1 2 3 4 5 6 7 8 9 10 11
0 0 30 60 68 53 81 60 10 49 28 78 93
1 18 86 70 14 10 51 94 10 9 46 14 19
2 62 58 42 79 83 71 22 44 7 52 3 76
12 13 14 15 16 17 18 19 20 21 22 23
0 7 3 49 84 24 92 6 54 52 60 52 71
1 17 78 90 81 22 65 50 93 28 83 71 22
2 37 28 54 63 93 14 8 54 55 73 0 26

12列ずつ表示されます。列数を変えたいときは、"%C 8"のように指定できます。もう一度、DataFrameを表示してから、やってみましょう。

jupyter
a
jupyter
%C 8
0 1 2 3 4 5 6 7
0 80 61 53 28 36 56 94 72
1 41 75 74 58 86 82 92 26
2 16 40 97 14 73 54 33 87
8 9 10 11 12 13 14 15
0 81 20 16 37 39 5 8 34
1 4 0 43 16 5 46 25 25
2 60 42 72 43 44 70 58 42
16 17 18 19 20 21 22 23
0 71 41 60 32 69 79 23 79
1 4 71 92 31 25 26 6 3
2 61 10 35 81 50 60 68 81

第2引数で行数を変えることもできます。

jupyter
a
jupyter
%C 8 2
0 1 2 3 4 5 6 7
0 80 61 53 28 36 56 94 72
1 41 75 74 58 86 82 92 26
8 9 10 11 12 13 14 15
0 81 20 16 37 39 5 8 34
1 4 0 43 16 5 46 25 25
16 17 18 19 20 21 22 23
0 71 41 60 32 69 79 23 79
1 4 71 92 31 25 26 6 3

ジェネレーターの展開

ジェネレーターをそのまま見ると、中味がわかりません。"list(_)"とすれば、いいのですが、2文字で済むようにしてみました。

jupyter
(i for i in range(10))
>>>
<generator object <genexpr> at 0x000000000XXXX>
jupyter
%L
>>>
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

Notebookの横幅を広げる

jupyter
%%HTML
<style>
    div#notebook-container    { width: 95%; }
    div#menubar-container     { width: 65%; }
    div#maintoolbar-container { width: 99%; }
</style>

DataFrameのインデックスを非表示に

jupyter
df.style.set_table_styles([{'selector': 'tbody th', 'props': [('visibility','hidden')]}])

参考

以上

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
26