jupyterで、styleつきdataframeの出力方法を調べるのに大変手間取ったので、結果をまとめておく。
jupyterでは、cellの最後に記述されている出力指定のない変数は、以下のように出力される。
1
又、dataframeにはstyle で、文字の色や背景色が指定できる。
指定したものを出力する場合、
- cell内最後に指定したdataframeの変数名を記述(22行)
- displayを使う(21行)
を試みたが、displayでは、表にはなったがstyleが反映されなかった。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
import pandas as pd
from IPython.display import display
def color_red(val):
"""
Takes a scalar and returns a string with
the css property `'color: red'` for negative
strings, black otherwise.
"""
if val > 1000:
color = 'red'
else:
color = 'black'
return 'color: %s' % color
df = pd.DataFrame({'Date': ['2018/05/01','2018/05/02','2018/05/03','2018/05/04' ],
'val1': [1000, 800.1, 1200, 3000],
'val2':[900,900,1100,3100]})
s = df.style.applymap(color_red, subset=['val1'])
display(df)
s
|
display(df)の出力
.dataframe tbody tr th {
vertical-align: top;
}
.dataframe thead th {
text-align: right;
}
sの出力
以下のように、cellの途中でdataframeを出力したい場合,styleの反映ができない。(23行目)
当然、途中で変数名だけ記述した場合(24行目)は何も出力されない。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
|
import pandas as pd
from IPython.display import display
def color_red(val):
"""
Takes a scalar and returns a string with
the css property `'color: red'` for negative
strings, black otherwise.
"""
if val > 1000:
color = 'red'
else:
color = 'black'
return 'color: %s' % color
df = pd.DataFrame({'Date': ['2018/05/01','2018/05/02','2018/05/03','2018/05/04' ],
'val1': [1000, 800.1, 1200, 3000],
'val2':[900,900,1100,3100]})
s = df.style.applymap(color_red, subset=['val1'])
for i in [0,1]:
display(df)
s
s
|
.dataframe tbody tr th {
vertical-align: top;
}
.dataframe thead th {
text-align: right;
}
.dataframe tbody tr th {
vertical-align: top;
}
.dataframe thead th {
text-align: right;
}
以下のコードを、先頭で使用することでprint,displayなしで変数のみ記述した場合、
cell最後に記述したのと同じように、出力される事が確認できた。
但し、1度目に実行では反映されず2度目以降反映された。
from IPython.core.interactiveshell import InteractiveShell
InteractiveShell.ast_node_interactivity = "all"
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
|
import pandas as pd
from IPython.display import display
from IPython.core.interactiveshell import InteractiveShell
InteractiveShell.ast_node_interactivity = "all"
def color_red(val):
"""
Takes a scalar and returns a string with
the css property `'color: red'` for negative
strings, black otherwise.
"""
if val > 1000:
color = 'red'
else:
color = 'black'
return 'color: %s' % color
df = pd.DataFrame({'Date': ['2018/05/01','2018/05/02','2018/05/03','2018/05/04' ],
'val1': [1000, 800.1, 1200, 3000],
'val2':[900,900,1100,3100]})
s = df.style.applymap(color_red, subset=['val1'])
for i in [0,1]:
s
s
|
上記指定をjupyter のデフォルトにしたい場合、
下記内容の ~/.ipython/profile_default/ipython_config.py を作成する。
こちらは、最初から反映されます。
但し、変数もしくは、出力が割り当てられてないステートメントが
すべて出力されるので場合によっては見にくくなってしまう。
c = get_config()
# Run all nodes interactively
c.InteractiveShell.ast_node_interactivity = "all"
参考 : [Pretty Display of Variables] (https://www.dataquest.io/blog/jupyter-notebook-tips-tricks-shortcuts/)
テスト環境
- Lubuntu 18.04
- python 3.6.7
- Jupyter Notebook 5.2.1