{ "cells": [ { "cell_type": "code", "execution_count": 1, "metadata": { "ExecuteTime": { "end_time": "2019-09-15T21:37:25.493430Z", "start_time": "2019-09-15T21:37:25.452747Z" } }, "outputs": [ { "data": { "text/plain": [ "'0.25.1'" ] }, "execution_count": 1, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import pandas as pd\n", "import numpy as np\n", "\n", "pd.__version__" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# 最初の一歩" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## スタイル設定しないまま表示" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "ExecuteTime": { "end_time": "2019-09-15T21:37:29.389192Z", "start_time": "2019-09-15T21:37:29.344305Z" }, "scrolled": false }, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
ArakiBabaChou
index
国語758095
数学558070
理科809085
社会903070
\n", "
" ], "text/plain": [ " Araki Baba Chou\n", "index \n", "国語 75 80 95\n", "数学 55 80 70\n", "理科 80 90 85\n", "社会 90 30 70" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import pandas as pd\n", "# テストの成績表を読み込む\n", "df = pd.read_csv('../data/exam.csv', index_col='index')\n", "df" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "ExecuteTime": { "end_time": "2019-09-15T21:37:30.374818Z", "start_time": "2019-09-15T21:37:29.815213Z" } }, "outputs": [ { "data": { "text/html": [ "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
Araki Baba Chou
index
国語758095
数学558070
理科809085
社会903070
" ], "text/plain": [ "" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "def color_red_akaten(val):\n", " color = 'red' if val < 60 else 'black'\n", " return f'color: {color}'\n", "\n", "# テストの成績表を読み込む\n", "df = pd.read_csv('../data/exam.csv', index_col='index')\n", "df.style.applymap(color_red_akaten)" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "ExecuteTime": { "end_time": "2019-09-15T21:37:30.651236Z", "start_time": "2019-09-15T21:37:30.609457Z" } }, "outputs": [ { "data": { "text/html": [ "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
Araki Baba Chou
index
国語758095
数学558070
理科809085
社会903070
" ], "text/plain": [ "" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "def highlight_akaten(val):\n", " color = 'yellow' if val < 60 else 'None'\n", " return f'background-color: {color}'\n", "\n", "df.style.applymap(highlight_akaten)" ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "ExecuteTime": { "end_time": "2019-09-15T21:37:30.881259Z", "start_time": "2019-09-15T21:37:30.838541Z" } }, "outputs": [ { "data": { "text/html": [ "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
Araki Baba Chou
index
国語758095
数学55nan70
理科809085
社会903070
" ], "text/plain": [ "" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "def fill_nan(val):\n", " bg_color = 'gray' if pd.isnull(val) else 'None'\n", " color = 'white' if pd.isnull(val) else 'black'\n", " return f'background-color: {bg_color}; color: {color}'\n", "\n", "# 欠損値を含むデータを読み込む\n", "df = pd.read_csv('../data/exam_with_nan.csv', index_col='index')\n", "df.style.applymap(fill_nan)" ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "ExecuteTime": { "end_time": "2019-09-15T21:37:31.233593Z", "start_time": "2019-09-15T21:37:31.223291Z" } }, "outputs": [ { "data": { "text/plain": [ "pandas.io.formats.style.Styler" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "type(df.style)" ] }, { "cell_type": "code", "execution_count": 7, "metadata": { "ExecuteTime": { "end_time": "2019-09-15T21:37:31.623190Z", "start_time": "2019-09-15T21:37:31.609245Z" } }, "outputs": [ { "data": { "text/html": [ "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
Araki Baba Chou
index
国語758095
数学55nan70
理科809085
社会903070
" ], "text/plain": [ "" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "df.style" ] }, { "cell_type": "code", "execution_count": 8, "metadata": { "ExecuteTime": { "end_time": "2019-09-15T21:37:32.009724Z", "start_time": "2019-09-15T21:37:31.997198Z" } }, "outputs": [ { "data": { "text/plain": [ "['',\n", " ' ',\n", " ' ',\n", " ' ',\n", " ' ',\n", " ' ',\n", " ' ',\n", " ' ',\n", " ' ',\n", " ' ',\n", " ' ',\n", " ' ',\n", " ' ',\n", " ' ',\n", " ' ',\n", " ' ',\n", " ' ',\n", " ' ',\n", " ' ']" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# のぞいてみよう\n", "df.style.render().split('\\n')[:20]" ] }, { "cell_type": "code", "execution_count": 9, "metadata": { "ExecuteTime": { "end_time": "2019-09-15T21:37:32.413736Z", "start_time": "2019-09-15T21:37:32.381559Z" } }, "outputs": [ { "data": { "text/plain": [ "['
Araki Baba Chou
index
国語758095
数学55nan70
理科809085
\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
Araki Baba Chou
index
国語758095
数学55nan70
理科809085
社会903070
" ], "text/plain": [ "" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "df.style.applymap(fill_nan).applymap(color_red_akaten)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 最後に適用したCSSが優先される" ] }, { "cell_type": "code", "execution_count": 11, "metadata": { "ExecuteTime": { "end_time": "2019-09-15T21:37:33.597148Z", "start_time": "2019-09-15T21:37:33.550312Z" } }, "outputs": [ { "data": { "text/html": [ "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
Araki Baba Chou
index
国語758095
数学55nan70
理科809085
社会903070
" ], "text/plain": [ "" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "def fill_nan_1(val):\n", " # 背景を灰色、文字を白にする\n", " bg_color = 'gray' if pd.isnull(val) else 'None'\n", " color = 'white' if pd.isnull(val) else 'black'\n", " return f'background-color: {bg_color}; color: {color}'\n", "\n", "def fill_nan_2(val):\n", " # 背景を黒、文字を黄色にする\n", " bg_color = 'black' if pd.isnull(val) else 'None'\n", " color = 'yellow' if pd.isnull(val) else 'black'\n", " return f'background-color: {bg_color}; color: {color}'\n", "\n", "df.style.applymap(fill_nan_1).applymap(fill_nan_2)" ] }, { "cell_type": "code", "execution_count": 12, "metadata": { "ExecuteTime": { "end_time": "2019-09-15T21:37:34.131969Z", "start_time": "2019-09-15T21:37:34.090819Z" } }, "outputs": [ { "data": { "text/plain": [ "['\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
Araki Baba Chou
index
国語758095
数学55nan70
理科809085
社会903070
" ], "text/plain": [ "" ] }, "execution_count": 15, "metadata": {}, "output_type": "execute_result" } ], "source": [ "def highlight_green_max(series):\n", " # 最大値だったらlightgreen, それ以外だったらnone(スタイルなし)にする\n", " color_list = ['lightgreen' if val == series.max() else 'none'\n", " for val in series]\n", "\n", " return [f'background-color: {color}' for color in color_list]\n", "\n", "# axis=1だと、列全体を見る (axis=0だと行全体を見る)\n", "df.style.apply(highlight_green_max, axis=1)" ] }, { "cell_type": "code", "execution_count": 16, "metadata": { "ExecuteTime": { "end_time": "2019-09-15T21:37:40.133078Z", "start_time": "2019-09-15T21:37:40.100773Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "['none', 'none', 'lightgreen']\n", "['none', 'none', 'lightgreen']\n", "['none', 'lightgreen', 'none']\n", "['lightgreen', 'none', 'none']\n" ] }, { "data": { "text/html": [ "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
Araki Baba Chou
index
国語758095
数学55nan70
理科809085
社会903070
" ], "text/plain": [ "" ] }, "execution_count": 16, "metadata": {}, "output_type": "execute_result" } ], "source": [ "def highlight_green_max(series):\n", " # 最大値だったらlightgreen, それ以外だったらnone(スタイルなし)にする\n", " color_list = ['lightgreen' if val == series.max() else 'none'\n", " for val in series]\n", " \n", " # 中身の確認(解説用)\n", " print(color_list)\n", " \n", " return [f'background-color: {color}' for color in color_list]\n", "\n", "# axis=1だと、列全体を見る (axis=0だと行全体を見る)\n", "df.style.apply(highlight_green_max, axis=1)" ] }, { "cell_type": "code", "execution_count": 17, "metadata": { "ExecuteTime": { "end_time": "2019-09-15T21:18:05.246134Z", "start_time": "2019-09-15T21:18:05.193991Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "['none', 'none', 'none', 'lightgreen']\n", "['none', 'none', 'lightgreen', 'none']\n", "['lightgreen', 'none', 'none', 'none']\n" ] }, { "data": { "text/html": [ "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
Araki Baba Chou
index
国語758095
数学55nan70
理科809085
社会903070
" ], "text/plain": [ "" ] }, "execution_count": 17, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# 各生徒において一番成績がよかった教科はどれ?\n", "df.style.apply(highlight_green_max, axis=0) # axis=0だと、列全体を見る" ] }, { "cell_type": "code", "execution_count": 18, "metadata": { "ExecuteTime": { "end_time": "2019-09-15T21:37:42.989397Z", "start_time": "2019-09-15T21:37:42.957583Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "['none', 'none', 'lightgreen']\n", "['none', 'none', 'lightgreen']\n", "['none', 'lightgreen', 'none']\n", "['lightgreen', 'none', 'none']\n" ] }, { "data": { "text/html": [ "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
Araki Baba Chou
index
国語758095
数学55nan70
理科809085
社会903070
" ], "text/plain": [ "" ] }, "execution_count": 18, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# axis=1だと、列全体を見る\n", "df.style.apply(highlight_green_max, axis=1)" ] }, { "cell_type": "markdown", "metadata": { "ExecuteTime": { "end_time": "2019-09-15T21:41:53.898233Z", "start_time": "2019-09-15T21:41:53.857669Z" } }, "source": [ "テーブル全体を見る場合は、`axis=None`にする。" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# `applymap`と`apply`のまとめ\n", "" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Quiz\n", "- 以下の場合、applymapとapplyのどちらを使えばよいか考えてみよう\n", " - 値がnullだったら文字色を灰色にする\n", " - その人が一番点数が低い教科の背景色を変える" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# ビルトインのスタイル関数\n", "## よく使うやつは、あらかじめ準備されている" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### highlight_max, highlight_min" ] }, { "cell_type": "code", "execution_count": 19, "metadata": { "ExecuteTime": { "end_time": "2019-09-15T22:04:02.134731Z", "start_time": "2019-09-15T22:04:01.894224Z" } }, "outputs": [ { "data": { "text/html": [ "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
Araki Baba Chou
index
国語758095
数学55nan70
理科809085
社会903070
" ], "text/plain": [ "" ] }, "execution_count": 19, "metadata": {}, "output_type": "execute_result" } ], "source": [ "df.style.highlight_max()" ] }, { "cell_type": "code", "execution_count": 20, "metadata": { "ExecuteTime": { "end_time": "2019-09-15T22:04:13.957232Z", "start_time": "2019-09-15T22:04:13.916944Z" } }, "outputs": [ { "data": { "text/html": [ "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
Araki Baba Chou
index
国語758095
数学55nan70
理科809085
社会903070
" ], "text/plain": [ "" ] }, "execution_count": 20, "metadata": {}, "output_type": "execute_result" } ], "source": [ "df.style.highlight_min()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### highlight_null" ] }, { "cell_type": "code", "execution_count": 21, "metadata": { "ExecuteTime": { "end_time": "2019-09-15T22:35:02.937493Z", "start_time": "2019-09-15T22:35:02.851168Z" } }, "outputs": [], "source": [ "sample_df = pd.read_csv('../data/styling_sample_02.csv')" ] }, { "cell_type": "code", "execution_count": 22, "metadata": { "ExecuteTime": { "end_time": "2019-09-15T22:35:03.757575Z", "start_time": "2019-09-15T22:35:03.665882Z" } }, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
AB
00.0NaN
110.080.0
220.060.0
330.040.0
440.020.0
5NaN0.0
660.0-20.0
770.0-40.0
880.0-60.0
9100.0-80.0
\n", "
" ], "text/plain": [ " A B\n", "0 0.0 NaN\n", "1 10.0 80.0\n", "2 20.0 60.0\n", "3 30.0 40.0\n", "4 40.0 20.0\n", "5 NaN 0.0\n", "6 60.0 -20.0\n", "7 70.0 -40.0\n", "8 80.0 -60.0\n", "9 100.0 -80.0" ] }, "execution_count": 22, "metadata": {}, "output_type": "execute_result" } ], "source": [ "sample_df" ] }, { "cell_type": "code", "execution_count": 23, "metadata": { "ExecuteTime": { "end_time": "2019-09-15T22:35:07.333109Z", "start_time": "2019-09-15T22:35:07.173640Z" } }, "outputs": [ { "data": { "text/html": [ "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
A B
00nan
11080
22060
33040
44020
5nan0
660-20
770-40
880-60
9100-80
" ], "text/plain": [ "" ] }, "execution_count": 23, "metadata": {}, "output_type": "execute_result" } ], "source": [ "sample_df.style.highlight_null()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### bar" ] }, { "cell_type": "code", "execution_count": 24, "metadata": { "ExecuteTime": { "end_time": "2019-09-15T22:35:24.119212Z", "start_time": "2019-09-15T22:35:24.073159Z" } }, "outputs": [ { "data": { "text/html": [ "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
A B
00nan
11080
22060
33040
44020
5nan0
660-20
770-40
880-60
9100-80
" ], "text/plain": [ "" ] }, "execution_count": 24, "metadata": {}, "output_type": "execute_result" } ], "source": [ "sample_df = pd.read_csv('../data/styling_sample_02.csv')\n", "sample_df.style.bar()" ] }, { "cell_type": "code", "execution_count": 25, "metadata": { "ExecuteTime": { "end_time": "2019-09-15T22:38:17.120340Z", "start_time": "2019-09-15T22:38:17.011134Z" } }, "outputs": [ { "data": { "text/html": [ "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
A B
00nan
11080
22060
33040
44020
5nan0
660-20
770-40
880-60
9100-80
" ], "text/plain": [ "" ] }, "execution_count": 25, "metadata": {}, "output_type": "execute_result" } ], "source": [ "sample_df.style.bar(align='mid')" ] }, { "cell_type": "code", "execution_count": 26, "metadata": { "ExecuteTime": { "end_time": "2019-09-15T22:36:14.087244Z", "start_time": "2019-09-15T22:36:14.051381Z" } }, "outputs": [ { "data": { "text/html": [ "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
A B
00nan
11080
22060
33040
44020
5nan0
660-20
770-40
880-60
9100-80
" ], "text/plain": [ "" ] }, "execution_count": 26, "metadata": {}, "output_type": "execute_result" } ], "source": [ "sample_df.style.bar(align='mid', color='skyblue')" ] }, { "cell_type": "code", "execution_count": 27, "metadata": { "ExecuteTime": { "end_time": "2019-09-15T22:36:14.610263Z", "start_time": "2019-09-15T22:36:14.563051Z" } }, "outputs": [ { "data": { "text/html": [ "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
A B
00nan
11080
22060
33040
44020
5nan0
660-20
770-40
880-60
9100-80
" ], "text/plain": [ "" ] }, "execution_count": 27, "metadata": {}, "output_type": "execute_result" } ], "source": [ "sample_df.style.bar(align='mid', color=['red', 'lightgreen'])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# スタイルを適用する範囲を絞る" ] }, { "cell_type": "code", "execution_count": 28, "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
A B
00nan
11080
22060
33040
44020
5nan0
660-20
770-40
880-60
9100-80
" ], "text/plain": [ "" ] }, "execution_count": 28, "metadata": {}, "output_type": "execute_result" } ], "source": [ "sample_df.style.bar(subset=['A'])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# キャプションをつける(`set_caption`)" ] }, { "cell_type": "code", "execution_count": 29, "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
期末試験の結果
Araki Baba Chou
index
国語758095
数学55nan70
理科809085
社会903070
" ], "text/plain": [ "" ] }, "execution_count": 29, "metadata": {}, "output_type": "execute_result" } ], "source": [ "df.style.applymap(color_red_akaten).set_caption(\"期末試験の結果\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Excelにスタイル付でエクスポートする" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## to_excel\n", "openpyxlを別途インストールする必要があります。" ] }, { "cell_type": "code", "execution_count": 30, "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
Araki Baba Chou
index
国語758095
数学55nan70
理科809085
社会903070
" ], "text/plain": [ "" ] }, "execution_count": 30, "metadata": {}, "output_type": "execute_result" } ], "source": [ "df.style.highlight_max()" ] }, { "cell_type": "code", "execution_count": 31, "metadata": {}, "outputs": [], "source": [ "df.style.highlight_max().to_excel('exam_df.xlsx')" ] }, { "cell_type": "code", "execution_count": 32, "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
A B
00nan
11080
22060
33040
44020
5nan0
660-20
770-40
880-60
9100-80
" ], "text/plain": [ "" ] }, "execution_count": 32, "metadata": {}, "output_type": "execute_result" } ], "source": [ "sample_df.style.bar()" ] }, { "cell_type": "code", "execution_count": 33, "metadata": {}, "outputs": [], "source": [ "sample_df.style.bar().to_excel('bar_df.xlsx')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Stylingの注意点" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "- まだprovisinal\n", "- GitHubにupしたときはスタイルが消える\n", " - nbviewerを使うとスタイルが保持されるよ\n", " - nbviewer: https://nbviewer.jupyter.org/\n", " - githubリポジトリ上のipynbのurlを指定すると描画してくれる(JavaScriptの動作も反映される)\n", " - このノートブックの場合: https://nbviewer.jupyter.org/github/komo-fr/PyConJP2019_pandas_styling/blob/master/notebooks/styling.ipynb\n", "- 変なCSSを当てても例外は発生しない" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 変なCSSを当ててみる" ] }, { "cell_type": "code", "execution_count": 34, "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
Araki Baba Chou
index
国語758095
数学55nan70
理科809085
社会903070
" ], "text/plain": [ "" ] }, "execution_count": 34, "metadata": {}, "output_type": "execute_result" } ], "source": [ "def strange_css(val):\n", " return 'background-color: yellowwwwww'\n", "\n", "# 例外は発生しない\n", "df.style.applymap(strange_css)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# より詳しくは\n", "**User Guide** \n", "https://pandas.pydata.org/pandas-docs/stable/user_guide/style.html \n", "\n", "**API Reference** \n", "https://pandas.pydata.org/pandas-docs/stable/reference/style.html" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Appendix" ] }, { "cell_type": "code", "execution_count": 36, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
ラベル教師データ数
04800
13000
2ねずみ1000
3うさぎ500
4キリン480
5パンダ300
670
7トカゲ30
\n", "
" ], "text/plain": [ " ラベル 教師データ数\n", "0 猫 4800\n", "1 犬 3000\n", "2 ねずみ 1000\n", "3 うさぎ 500\n", "4 キリン 480\n", "5 パンダ 300\n", "6 蛇 70\n", "7 トカゲ 30" ] }, "execution_count": 36, "metadata": {}, "output_type": "execute_result" } ], "source": [ "exp_df = pd.read_csv('../data/example.csv')\n", "exp_df" ] }, { "cell_type": "code", "execution_count": 37, "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
ラベル 教師データ数
04800
13000
2ねずみ1000
3うさぎ500
4キリン480
5パンダ300
670
7トカゲ30
" ], "text/plain": [ "" ] }, "execution_count": 37, "metadata": {}, "output_type": "execute_result" } ], "source": [ "def is_small_data(val):\n", " if type(val) == int:\n", " color = 'red' if val < 100 else 'black'\n", " else:\n", " return ''\n", " return f'color: {color}'\n", "exp_df.style.bar(color='skyblue').applymap(is_small_data)" ] } ], "metadata": { "hide_input": false, "kernelspec": { "display_name": "Environment (conda_pepmap_env)", "language": "python", "name": "conda_pepmap_env" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.6.2" }, "nbTranslate": { "displayLangs": [ "*" ], "hotkey": "alt-t", "langInMainMenu": true, "sourceLang": "en", "targetLang": "fr", "useGoogleTranslate": true }, "toc": { "base_numbering": 1, "nav_menu": {}, "number_sections": true, "sideBar": true, "skip_h1_title": false, "title_cell": "Table of Contents", "title_sidebar": "Contents", "toc_cell": false, "toc_position": {}, "toc_section_display": true, "toc_window_display": false }, "varInspector": { "cols": { "lenName": 16, "lenType": 16, "lenVar": 40 }, "kernels_config": { "python": { "delete_cmd_postfix": "", "delete_cmd_prefix": "del ", "library": "var_list.py", "varRefreshCmd": "print(var_dic_list())" }, "r": { "delete_cmd_postfix": ") ", "delete_cmd_prefix": "rm(", "library": "var_list.r", "varRefreshCmd": "cat(var_dic_list()) " } }, "types_to_exclude": [ "module", "function", "builtin_function_or_method", "instance", "_Feature" ], "window_display": false } }, "nbformat": 4, "nbformat_minor": 2 }