redoules.github.io/python/style_dataframe.html
Guillaume 44f740504b added an article
about uploading data to a sharepoint site
2020-07-20 20:20:09 +02:00

689 lines
47 KiB
HTML

<!DOCTYPE html>
<html lang="fr">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<!-- The above 3 meta tags *must* come first in the head; any other head content must come *after* these tags -->
<meta name="description" content="Data Science for Political and Social Phenomena">
<meta name="author" content="Guillaume Redoulès">
<link rel="icon" href="../favicon.ico">
<title>Style a dataframe - Python</title>
<!-- JQuery -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script>
window.jQuery || document.write('<script src="../theme/js/jquery.min.js"><\/script>')
</script>
<!-- Bootstrap core CSS -->
<link rel="stylesheet" href="../theme/css/bootstrap.css" />
<!-- IE10 viewport hack for Surface/desktop Windows 8 bug -->
<link rel="stylesheet" type="text/css" href="../theme/css/ie10-viewport-bug-workaround.css" />
<!-- Custom styles for this template -->
<link rel="stylesheet" type="text/css" href="../theme/css/style.css" />
<link rel="stylesheet" type="text/css" href="../theme/css/notebooks.css" />
<link href='https://fonts.googleapis.com/css?family=PT+Serif:400,700|Roboto:400,500,700' rel='stylesheet' type='text/css'>
<!-- HTML5 shim and Respond.js for IE8 support of HTML5 elements and media queries -->
<!--[if lt IE 9]>
<script src="https://oss.maxcdn.com/html5shiv/3.7.2/html5shiv.min.js"></script>
<script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
<![endif]-->
<meta name="tags" content="Data Wrangling" />
</head>
<body>
<div class="navbar navbar-fixed-top">
<div class="container">
<div class="navbar-header">
<button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse">
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a class="navbar-brand" href="..">Guillaume Redoulès</a>
</div>
<div class="navbar-collapse collapse" id="searchbar">
<ul class="nav navbar-nav navbar-right">
<li class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">About<span class="caret"></span></a>
<ul class="dropdown-menu">
<li><a href="../pages/about.html">About Guillaume</a></li>
<li><a href="https://github.com/redoules">GitHub</a></li>
<li><a href="https://www.linkedin.com/in/guillaume-redoul%C3%A8s-33923860/">LinkedIn</a></li>
</ul>
</li>
<li class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">Data Science<span class="caret"></span></a>
<ul class="dropdown-menu">
<li><a href="..#Blog">Blog</a></li>
<li><a href="..#Python">Python</a></li>
<li><a href="..#Bash">Bash</a></li>
<li><a href="..#SQL">SQL</a></li>
<li><a href="..#Mathematics">Mathematics</a></li>
<li><a href="..#Machine_Learning">Machine Learning</a></li>
<li><a href="..#Projects">Projects</a></li>
</ul>
</li>
<li class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">Projects<span class="caret"></span></a>
<ul class="dropdown-menu">
<li><a href="https://github.com/redoules/redoules.github.io">Notes (Github)</a></li>
</ul>
</li>
<!--<li class="dropdown">
<a href="../feeds/blog.rss.xml">Blog RSS</a>
</li>-->
</ul>
<form class="navbar-form" action="../search.html" onsubmit="return validateForm(this.elements['q'].value);">
<div class="form-group" style="display:inline;">
<div class="input-group" style="display:table;">
<span class="input-group-addon" style="width:1%;"><span class="glyphicon glyphicon-search"></span></span>
<input class="form-control search-query" name="q" id="tipue_search_input" placeholder="e.g. scikit KNN, pandas merge" required autocomplete="off" type="text">
</div>
</div>
</form>
</div>
<!--/.nav-collapse -->
</div>
</div>
<!-- end of header section -->
<div class="container">
<!-- <div class="alert alert-warning" role="alert">
Did you find this page useful? Please do me a quick favor and <a href="#" class="alert-link">endorse me for data science on LinkedIn</a>.
</div> -->
<section id="content" class="body">
<header>
<h1>
Style a dataframe
</h1>
<ol class="breadcrumb">
<li>
<time class="published" datetime="2019-12-24T10:56:00+01:00">
24 décembre 2019
</time>
</li>
<li>Python</li>
<li>Data Wrangling</li>
</ol>
</header>
<div class='article_content'>
<p>DataFrame output in the notebook can be personalised with some CSS syntaxe applied to the <code>style</code> attribute.</p>
<h2>Basic styling</h2>
<p>Basic styling, color nan values in red, max values in blue for each Line and the min value in gold for each column.</p>
<div class="highlight"><pre><span></span><span class="c1"># Import modules</span>
<span class="kn">import</span> <span class="nn">pandas</span> <span class="k">as</span> <span class="nn">pd</span>
<span class="c1"># Example dataframe</span>
<span class="n">raw_data</span> <span class="o">=</span> <span class="p">{</span><span class="s1">&#39;fruit&#39;</span><span class="p">:</span> <span class="p">[</span><span class="s1">&#39;Banana&#39;</span><span class="p">,</span> <span class="s1">&#39;Orange&#39;</span><span class="p">,</span> <span class="s1">&#39;Apple&#39;</span><span class="p">,</span> <span class="s1">&#39;lemon&#39;</span><span class="p">,</span> <span class="s2">&quot;lime&quot;</span><span class="p">,</span> <span class="s2">&quot;plum&quot;</span><span class="p">],</span>
<span class="s1">&#39;color&#39;</span><span class="p">:</span> <span class="p">[</span><span class="s1">&#39;yellow&#39;</span><span class="p">,</span> <span class="s1">&#39;orange&#39;</span><span class="p">,</span> <span class="s1">&#39;red&#39;</span><span class="p">,</span> <span class="s1">&#39;yellow&#39;</span><span class="p">,</span> <span class="s2">&quot;green&quot;</span><span class="p">,</span> <span class="s2">&quot;purple&quot;</span><span class="p">],</span>
<span class="s1">&#39;kcal&#39;</span><span class="p">:</span> <span class="p">[</span><span class="mi">89</span><span class="p">,</span> <span class="mi">47</span><span class="p">,</span> <span class="mi">52</span><span class="p">,</span> <span class="mi">15</span><span class="p">,</span> <span class="mi">30</span><span class="p">,</span> <span class="mi">28</span><span class="p">],</span>
<span class="s1">&#39;size_cm&#39;</span> <span class="p">:</span> <span class="p">[</span><span class="mi">20</span><span class="p">,</span> <span class="mi">10</span><span class="p">,</span> <span class="mi">9</span><span class="p">,</span> <span class="mi">7</span><span class="p">,</span> <span class="kc">None</span><span class="p">,</span> <span class="mi">4</span><span class="p">]</span>
<span class="p">}</span>
<span class="n">df</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">DataFrame</span><span class="p">(</span><span class="n">raw_data</span><span class="p">,</span> <span class="n">columns</span> <span class="o">=</span> <span class="p">[</span><span class="s1">&#39;fruit&#39;</span><span class="p">,</span> <span class="s1">&#39;color&#39;</span><span class="p">,</span> <span class="s1">&#39;kcal&#39;</span><span class="p">,</span> <span class="s2">&quot;size_cm&quot;</span><span class="p">])</span>
<span class="p">(</span><span class="n">df</span><span class="o">.</span><span class="n">style</span>
<span class="o">.</span><span class="n">highlight_null</span><span class="p">(</span><span class="s1">&#39;red&#39;</span><span class="p">)</span>
<span class="o">.</span><span class="n">highlight_max</span><span class="p">(</span><span class="n">color</span><span class="o">=</span><span class="s1">&#39;steelblue&#39;</span><span class="p">,</span> <span class="n">axis</span><span class="o">=</span><span class="mi">0</span><span class="p">)</span>
<span class="o">.</span><span class="n">highlight_min</span><span class="p">(</span><span class="n">color</span><span class="o">=</span><span class="s1">&#39;gold&#39;</span><span class="p">,</span><span class="n">axis</span><span class="o">=</span><span class="mi">1</span><span class="p">)</span>
<span class="p">)</span>
</pre></div>
<style type="text/css" >
#T_104e16d6_2634_11ea_9473_edbe091121fcrow0_col2 {
: ;
background-color: steelblue;
: ;
} #T_104e16d6_2634_11ea_9473_edbe091121fcrow0_col3 {
: ;
background-color: steelblue;
background-color: gold;
} #T_104e16d6_2634_11ea_9473_edbe091121fcrow1_col3 {
: ;
: ;
background-color: gold;
} #T_104e16d6_2634_11ea_9473_edbe091121fcrow2_col3 {
: ;
: ;
background-color: gold;
} #T_104e16d6_2634_11ea_9473_edbe091121fcrow3_col3 {
: ;
: ;
background-color: gold;
} #T_104e16d6_2634_11ea_9473_edbe091121fcrow4_col2 {
: ;
: ;
background-color: gold;
} #T_104e16d6_2634_11ea_9473_edbe091121fcrow4_col3 {
background-color: red;
: ;
: ;
} #T_104e16d6_2634_11ea_9473_edbe091121fcrow5_col3 {
: ;
: ;
background-color: gold;
}</style>
<table id="T_104e16d6_2634_11ea_9473_edbe091121fc" ><thead> <tr> <th class="blank level0" ></th> <th class="col_heading level0 col0" >fruit</th> <th class="col_heading level0 col1" >color</th> <th class="col_heading level0 col2" >kcal</th> <th class="col_heading level0 col3" >size_cm</th> </tr></thead><tbody>
<tr>
<th id="T_104e16d6_2634_11ea_9473_edbe091121fclevel0_row0" class="row_heading level0 row0" >0</th>
<td id="T_104e16d6_2634_11ea_9473_edbe091121fcrow0_col0" class="data row0 col0" >Banana</td>
<td id="T_104e16d6_2634_11ea_9473_edbe091121fcrow0_col1" class="data row0 col1" >yellow</td>
<td id="T_104e16d6_2634_11ea_9473_edbe091121fcrow0_col2" class="data row0 col2" >89</td>
<td id="T_104e16d6_2634_11ea_9473_edbe091121fcrow0_col3" class="data row0 col3" >20</td>
</tr>
<tr>
<th id="T_104e16d6_2634_11ea_9473_edbe091121fclevel0_row1" class="row_heading level0 row1" >1</th>
<td id="T_104e16d6_2634_11ea_9473_edbe091121fcrow1_col0" class="data row1 col0" >Orange</td>
<td id="T_104e16d6_2634_11ea_9473_edbe091121fcrow1_col1" class="data row1 col1" >orange</td>
<td id="T_104e16d6_2634_11ea_9473_edbe091121fcrow1_col2" class="data row1 col2" >47</td>
<td id="T_104e16d6_2634_11ea_9473_edbe091121fcrow1_col3" class="data row1 col3" >10</td>
</tr>
<tr>
<th id="T_104e16d6_2634_11ea_9473_edbe091121fclevel0_row2" class="row_heading level0 row2" >2</th>
<td id="T_104e16d6_2634_11ea_9473_edbe091121fcrow2_col0" class="data row2 col0" >Apple</td>
<td id="T_104e16d6_2634_11ea_9473_edbe091121fcrow2_col1" class="data row2 col1" >red</td>
<td id="T_104e16d6_2634_11ea_9473_edbe091121fcrow2_col2" class="data row2 col2" >52</td>
<td id="T_104e16d6_2634_11ea_9473_edbe091121fcrow2_col3" class="data row2 col3" >9</td>
</tr>
<tr>
<th id="T_104e16d6_2634_11ea_9473_edbe091121fclevel0_row3" class="row_heading level0 row3" >3</th>
<td id="T_104e16d6_2634_11ea_9473_edbe091121fcrow3_col0" class="data row3 col0" >lemon</td>
<td id="T_104e16d6_2634_11ea_9473_edbe091121fcrow3_col1" class="data row3 col1" >yellow</td>
<td id="T_104e16d6_2634_11ea_9473_edbe091121fcrow3_col2" class="data row3 col2" >15</td>
<td id="T_104e16d6_2634_11ea_9473_edbe091121fcrow3_col3" class="data row3 col3" >7</td>
</tr>
<tr>
<th id="T_104e16d6_2634_11ea_9473_edbe091121fclevel0_row4" class="row_heading level0 row4" >4</th>
<td id="T_104e16d6_2634_11ea_9473_edbe091121fcrow4_col0" class="data row4 col0" >lime</td>
<td id="T_104e16d6_2634_11ea_9473_edbe091121fcrow4_col1" class="data row4 col1" >green</td>
<td id="T_104e16d6_2634_11ea_9473_edbe091121fcrow4_col2" class="data row4 col2" >30</td>
<td id="T_104e16d6_2634_11ea_9473_edbe091121fcrow4_col3" class="data row4 col3" >nan</td>
</tr>
<tr>
<th id="T_104e16d6_2634_11ea_9473_edbe091121fclevel0_row5" class="row_heading level0 row5" >5</th>
<td id="T_104e16d6_2634_11ea_9473_edbe091121fcrow5_col0" class="data row5 col0" >plum</td>
<td id="T_104e16d6_2634_11ea_9473_edbe091121fcrow5_col1" class="data row5 col1" >purple</td>
<td id="T_104e16d6_2634_11ea_9473_edbe091121fcrow5_col2" class="data row5 col2" >28</td>
<td id="T_104e16d6_2634_11ea_9473_edbe091121fcrow5_col3" class="data row5 col3" >4</td>
</tr>
</tbody></table>
<h2>Gradient</h2>
<p>Color the value of the dataframe with a color gradient based on the value of the cell</p>
<div class="highlight"><pre><span></span><span class="n">raw_data</span> <span class="o">=</span> <span class="p">{</span><span class="s1">&#39;fruit&#39;</span><span class="p">:</span> <span class="p">[</span><span class="s1">&#39;Banana&#39;</span><span class="p">,</span> <span class="s1">&#39;Orange&#39;</span><span class="p">,</span> <span class="s1">&#39;Apple&#39;</span><span class="p">,</span> <span class="s1">&#39;lemon&#39;</span><span class="p">,</span> <span class="s2">&quot;lime&quot;</span><span class="p">,</span> <span class="s2">&quot;plum&quot;</span><span class="p">],</span>
<span class="s1">&#39;color&#39;</span><span class="p">:</span> <span class="p">[</span><span class="s1">&#39;yellow&#39;</span><span class="p">,</span> <span class="s1">&#39;orange&#39;</span><span class="p">,</span> <span class="s1">&#39;red&#39;</span><span class="p">,</span> <span class="s1">&#39;yellow&#39;</span><span class="p">,</span> <span class="s2">&quot;green&quot;</span><span class="p">,</span> <span class="s2">&quot;purple&quot;</span><span class="p">],</span>
<span class="s1">&#39;kcal&#39;</span><span class="p">:</span> <span class="p">[</span><span class="mi">89</span><span class="p">,</span> <span class="mi">47</span><span class="p">,</span> <span class="mi">52</span><span class="p">,</span> <span class="mi">15</span><span class="p">,</span> <span class="mi">30</span><span class="p">,</span> <span class="mi">28</span><span class="p">],</span>
<span class="s1">&#39;size_cm&#39;</span> <span class="p">:</span> <span class="p">[</span><span class="mi">20</span><span class="p">,</span> <span class="mi">10</span><span class="p">,</span> <span class="mi">9</span><span class="p">,</span> <span class="mi">7</span><span class="p">,</span> <span class="mi">6</span><span class="p">,</span> <span class="mi">4</span><span class="p">]</span>
<span class="p">}</span>
<span class="n">df</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">DataFrame</span><span class="p">(</span><span class="n">raw_data</span><span class="p">,</span> <span class="n">columns</span> <span class="o">=</span> <span class="p">[</span><span class="s1">&#39;fruit&#39;</span><span class="p">,</span> <span class="s1">&#39;color&#39;</span><span class="p">,</span> <span class="s1">&#39;kcal&#39;</span><span class="p">,</span> <span class="s2">&quot;size_cm&quot;</span><span class="p">])</span>
<span class="n">df</span><span class="o">.</span><span class="n">style</span><span class="o">.</span><span class="n">background_gradient</span><span class="p">()</span>
</pre></div>
<style type="text/css" >
#T_2a7071d0_2634_11ea_9473_edbe091121fcrow0_col2 {
background-color: #023858;
color: #f1f1f1;
} #T_2a7071d0_2634_11ea_9473_edbe091121fcrow0_col3 {
background-color: #023858;
color: #f1f1f1;
} #T_2a7071d0_2634_11ea_9473_edbe091121fcrow1_col2 {
background-color: #8fb4d6;
color: #000000;
} #T_2a7071d0_2634_11ea_9473_edbe091121fcrow1_col3 {
background-color: #a5bddb;
color: #000000;
} #T_2a7071d0_2634_11ea_9473_edbe091121fcrow2_col2 {
background-color: #73a9cf;
color: #000000;
} #T_2a7071d0_2634_11ea_9473_edbe091121fcrow2_col3 {
background-color: #bbc7e0;
color: #000000;
} #T_2a7071d0_2634_11ea_9473_edbe091121fcrow3_col2 {
background-color: #fff7fb;
color: #000000;
} #T_2a7071d0_2634_11ea_9473_edbe091121fcrow3_col3 {
background-color: #dedcec;
color: #000000;
} #T_2a7071d0_2634_11ea_9473_edbe091121fcrow4_col2 {
background-color: #dbdaeb;
color: #000000;
} #T_2a7071d0_2634_11ea_9473_edbe091121fcrow4_col3 {
background-color: #ece7f2;
color: #000000;
} #T_2a7071d0_2634_11ea_9473_edbe091121fcrow5_col2 {
background-color: #e1dfed;
color: #000000;
} #T_2a7071d0_2634_11ea_9473_edbe091121fcrow5_col3 {
background-color: #fff7fb;
color: #000000;
}</style>
<table id="T_2a7071d0_2634_11ea_9473_edbe091121fc" ><thead> <tr> <th class="blank level0" ></th> <th class="col_heading level0 col0" >fruit</th> <th class="col_heading level0 col1" >color</th> <th class="col_heading level0 col2" >kcal</th> <th class="col_heading level0 col3" >size_cm</th> </tr></thead><tbody>
<tr>
<th id="T_2a7071d0_2634_11ea_9473_edbe091121fclevel0_row0" class="row_heading level0 row0" >0</th>
<td id="T_2a7071d0_2634_11ea_9473_edbe091121fcrow0_col0" class="data row0 col0" >Banana</td>
<td id="T_2a7071d0_2634_11ea_9473_edbe091121fcrow0_col1" class="data row0 col1" >yellow</td>
<td id="T_2a7071d0_2634_11ea_9473_edbe091121fcrow0_col2" class="data row0 col2" >89</td>
<td id="T_2a7071d0_2634_11ea_9473_edbe091121fcrow0_col3" class="data row0 col3" >20</td>
</tr>
<tr>
<th id="T_2a7071d0_2634_11ea_9473_edbe091121fclevel0_row1" class="row_heading level0 row1" >1</th>
<td id="T_2a7071d0_2634_11ea_9473_edbe091121fcrow1_col0" class="data row1 col0" >Orange</td>
<td id="T_2a7071d0_2634_11ea_9473_edbe091121fcrow1_col1" class="data row1 col1" >orange</td>
<td id="T_2a7071d0_2634_11ea_9473_edbe091121fcrow1_col2" class="data row1 col2" >47</td>
<td id="T_2a7071d0_2634_11ea_9473_edbe091121fcrow1_col3" class="data row1 col3" >10</td>
</tr>
<tr>
<th id="T_2a7071d0_2634_11ea_9473_edbe091121fclevel0_row2" class="row_heading level0 row2" >2</th>
<td id="T_2a7071d0_2634_11ea_9473_edbe091121fcrow2_col0" class="data row2 col0" >Apple</td>
<td id="T_2a7071d0_2634_11ea_9473_edbe091121fcrow2_col1" class="data row2 col1" >red</td>
<td id="T_2a7071d0_2634_11ea_9473_edbe091121fcrow2_col2" class="data row2 col2" >52</td>
<td id="T_2a7071d0_2634_11ea_9473_edbe091121fcrow2_col3" class="data row2 col3" >9</td>
</tr>
<tr>
<th id="T_2a7071d0_2634_11ea_9473_edbe091121fclevel0_row3" class="row_heading level0 row3" >3</th>
<td id="T_2a7071d0_2634_11ea_9473_edbe091121fcrow3_col0" class="data row3 col0" >lemon</td>
<td id="T_2a7071d0_2634_11ea_9473_edbe091121fcrow3_col1" class="data row3 col1" >yellow</td>
<td id="T_2a7071d0_2634_11ea_9473_edbe091121fcrow3_col2" class="data row3 col2" >15</td>
<td id="T_2a7071d0_2634_11ea_9473_edbe091121fcrow3_col3" class="data row3 col3" >7</td>
</tr>
<tr>
<th id="T_2a7071d0_2634_11ea_9473_edbe091121fclevel0_row4" class="row_heading level0 row4" >4</th>
<td id="T_2a7071d0_2634_11ea_9473_edbe091121fcrow4_col0" class="data row4 col0" >lime</td>
<td id="T_2a7071d0_2634_11ea_9473_edbe091121fcrow4_col1" class="data row4 col1" >green</td>
<td id="T_2a7071d0_2634_11ea_9473_edbe091121fcrow4_col2" class="data row4 col2" >30</td>
<td id="T_2a7071d0_2634_11ea_9473_edbe091121fcrow4_col3" class="data row4 col3" >6</td>
</tr>
<tr>
<th id="T_2a7071d0_2634_11ea_9473_edbe091121fclevel0_row5" class="row_heading level0 row5" >5</th>
<td id="T_2a7071d0_2634_11ea_9473_edbe091121fcrow5_col0" class="data row5 col0" >plum</td>
<td id="T_2a7071d0_2634_11ea_9473_edbe091121fcrow5_col1" class="data row5 col1" >purple</td>
<td id="T_2a7071d0_2634_11ea_9473_edbe091121fcrow5_col2" class="data row5 col2" >28</td>
<td id="T_2a7071d0_2634_11ea_9473_edbe091121fcrow5_col3" class="data row5 col3" >4</td>
</tr>
</tbody></table>
<h2>Custom style</h2>
<p>Create a custom style using CSS</p>
<div class="highlight"><pre><span></span><span class="k">def</span> <span class="nf">custom_style</span><span class="p">(</span><span class="n">val</span><span class="p">):</span>
<span class="k">if</span> <span class="n">val</span> <span class="o">&lt;</span> <span class="mi">5</span><span class="p">:</span>
<span class="k">return</span> <span class="s1">&#39;background-color:red&#39;</span>
<span class="k">elif</span> <span class="n">val</span> <span class="o">&gt;</span> <span class="mi">50</span><span class="p">:</span>
<span class="k">return</span> <span class="s1">&#39;background-color:green&#39;</span>
<span class="k">elif</span> <span class="nb">abs</span><span class="p">(</span><span class="n">val</span><span class="p">)</span> <span class="o">&lt;</span><span class="mi">20</span><span class="p">:</span>
<span class="k">return</span> <span class="s1">&#39;background-color:yellow&#39;</span>
<span class="k">else</span><span class="p">:</span>
<span class="k">return</span> <span class="s1">&#39;&#39;</span>
<span class="n">df</span><span class="p">[[</span><span class="s2">&quot;kcal&quot;</span><span class="p">,</span> <span class="s1">&#39;size_cm&#39;</span><span class="p">]]</span><span class="o">.</span><span class="n">style</span><span class="o">.</span><span class="n">applymap</span><span class="p">(</span><span class="n">custom_style</span><span class="p">)</span>
</pre></div>
<style type="text/css" >
#T_621776a6_2634_11ea_9473_edbe091121fcrow0_col0 {
background-color: green;
} #T_621776a6_2634_11ea_9473_edbe091121fcrow1_col1 {
background-color: yellow;
} #T_621776a6_2634_11ea_9473_edbe091121fcrow2_col0 {
background-color: green;
} #T_621776a6_2634_11ea_9473_edbe091121fcrow2_col1 {
background-color: yellow;
} #T_621776a6_2634_11ea_9473_edbe091121fcrow3_col0 {
background-color: yellow;
} #T_621776a6_2634_11ea_9473_edbe091121fcrow3_col1 {
background-color: yellow;
} #T_621776a6_2634_11ea_9473_edbe091121fcrow4_col1 {
background-color: yellow;
} #T_621776a6_2634_11ea_9473_edbe091121fcrow5_col1 {
background-color: red;
}</style>
<table id="T_621776a6_2634_11ea_9473_edbe091121fc" ><thead> <tr> <th class="blank level0" ></th> <th class="col_heading level0 col0" >kcal</th> <th class="col_heading level0 col1" >size_cm</th> </tr></thead><tbody>
<tr>
<th id="T_621776a6_2634_11ea_9473_edbe091121fclevel0_row0" class="row_heading level0 row0" >0</th>
<td id="T_621776a6_2634_11ea_9473_edbe091121fcrow0_col0" class="data row0 col0" >89</td>
<td id="T_621776a6_2634_11ea_9473_edbe091121fcrow0_col1" class="data row0 col1" >20</td>
</tr>
<tr>
<th id="T_621776a6_2634_11ea_9473_edbe091121fclevel0_row1" class="row_heading level0 row1" >1</th>
<td id="T_621776a6_2634_11ea_9473_edbe091121fcrow1_col0" class="data row1 col0" >47</td>
<td id="T_621776a6_2634_11ea_9473_edbe091121fcrow1_col1" class="data row1 col1" >10</td>
</tr>
<tr>
<th id="T_621776a6_2634_11ea_9473_edbe091121fclevel0_row2" class="row_heading level0 row2" >2</th>
<td id="T_621776a6_2634_11ea_9473_edbe091121fcrow2_col0" class="data row2 col0" >52</td>
<td id="T_621776a6_2634_11ea_9473_edbe091121fcrow2_col1" class="data row2 col1" >9</td>
</tr>
<tr>
<th id="T_621776a6_2634_11ea_9473_edbe091121fclevel0_row3" class="row_heading level0 row3" >3</th>
<td id="T_621776a6_2634_11ea_9473_edbe091121fcrow3_col0" class="data row3 col0" >15</td>
<td id="T_621776a6_2634_11ea_9473_edbe091121fcrow3_col1" class="data row3 col1" >7</td>
</tr>
<tr>
<th id="T_621776a6_2634_11ea_9473_edbe091121fclevel0_row4" class="row_heading level0 row4" >4</th>
<td id="T_621776a6_2634_11ea_9473_edbe091121fcrow4_col0" class="data row4 col0" >30</td>
<td id="T_621776a6_2634_11ea_9473_edbe091121fcrow4_col1" class="data row4 col1" >6</td>
</tr>
<tr>
<th id="T_621776a6_2634_11ea_9473_edbe091121fclevel0_row5" class="row_heading level0 row5" >5</th>
<td id="T_621776a6_2634_11ea_9473_edbe091121fcrow5_col0" class="data row5 col0" >28</td>
<td id="T_621776a6_2634_11ea_9473_edbe091121fcrow5_col1" class="data row5 col1" >4</td>
</tr>
</tbody></table>
<h2>Colorbars</h2>
<p>Draw bars in the cell based on the value in the cell</p>
<div class="highlight"><pre><span></span><span class="p">(</span><span class="n">df</span><span class="o">.</span><span class="n">style</span>
<span class="o">.</span><span class="n">bar</span><span class="p">(</span><span class="n">subset</span><span class="o">=</span><span class="p">[</span><span class="s1">&#39;kcal&#39;</span><span class="p">,</span><span class="s1">&#39;size_cm&#39;</span><span class="p">],</span><span class="n">color</span><span class="o">=</span><span class="s1">&#39;steelblue&#39;</span><span class="p">)</span>
<span class="p">)</span>
</pre></div>
<style type="text/css" >
#T_809fc9a2_2634_11ea_9473_edbe091121fcrow0_col2 {
width: 10em;
height: 80%;
background: linear-gradient(90deg,steelblue 100.0%, transparent 100.0%);
} #T_809fc9a2_2634_11ea_9473_edbe091121fcrow0_col3 {
width: 10em;
height: 80%;
background: linear-gradient(90deg,steelblue 100.0%, transparent 100.0%);
} #T_809fc9a2_2634_11ea_9473_edbe091121fcrow1_col2 {
width: 10em;
height: 80%;
background: linear-gradient(90deg,steelblue 43.2%, transparent 43.2%);
} #T_809fc9a2_2634_11ea_9473_edbe091121fcrow1_col3 {
width: 10em;
height: 80%;
background: linear-gradient(90deg,steelblue 37.5%, transparent 37.5%);
} #T_809fc9a2_2634_11ea_9473_edbe091121fcrow2_col2 {
width: 10em;
height: 80%;
background: linear-gradient(90deg,steelblue 50.0%, transparent 50.0%);
} #T_809fc9a2_2634_11ea_9473_edbe091121fcrow2_col3 {
width: 10em;
height: 80%;
background: linear-gradient(90deg,steelblue 31.2%, transparent 31.2%);
} #T_809fc9a2_2634_11ea_9473_edbe091121fcrow3_col2 {
width: 10em;
height: 80%;
} #T_809fc9a2_2634_11ea_9473_edbe091121fcrow3_col3 {
width: 10em;
height: 80%;
background: linear-gradient(90deg,steelblue 18.7%, transparent 18.7%);
} #T_809fc9a2_2634_11ea_9473_edbe091121fcrow4_col2 {
width: 10em;
height: 80%;
background: linear-gradient(90deg,steelblue 20.3%, transparent 20.3%);
} #T_809fc9a2_2634_11ea_9473_edbe091121fcrow4_col3 {
width: 10em;
height: 80%;
background: linear-gradient(90deg,steelblue 12.5%, transparent 12.5%);
} #T_809fc9a2_2634_11ea_9473_edbe091121fcrow5_col2 {
width: 10em;
height: 80%;
background: linear-gradient(90deg,steelblue 17.6%, transparent 17.6%);
} #T_809fc9a2_2634_11ea_9473_edbe091121fcrow5_col3 {
width: 10em;
height: 80%;
}</style>
<table id="T_809fc9a2_2634_11ea_9473_edbe091121fc" ><thead> <tr> <th class="blank level0" ></th> <th class="col_heading level0 col0" >fruit</th> <th class="col_heading level0 col1" >color</th> <th class="col_heading level0 col2" >kcal</th> <th class="col_heading level0 col3" >size_cm</th> </tr></thead><tbody>
<tr>
<th id="T_809fc9a2_2634_11ea_9473_edbe091121fclevel0_row0" class="row_heading level0 row0" >0</th>
<td id="T_809fc9a2_2634_11ea_9473_edbe091121fcrow0_col0" class="data row0 col0" >Banana</td>
<td id="T_809fc9a2_2634_11ea_9473_edbe091121fcrow0_col1" class="data row0 col1" >yellow</td>
<td id="T_809fc9a2_2634_11ea_9473_edbe091121fcrow0_col2" class="data row0 col2" >89</td>
<td id="T_809fc9a2_2634_11ea_9473_edbe091121fcrow0_col3" class="data row0 col3" >20</td>
</tr>
<tr>
<th id="T_809fc9a2_2634_11ea_9473_edbe091121fclevel0_row1" class="row_heading level0 row1" >1</th>
<td id="T_809fc9a2_2634_11ea_9473_edbe091121fcrow1_col0" class="data row1 col0" >Orange</td>
<td id="T_809fc9a2_2634_11ea_9473_edbe091121fcrow1_col1" class="data row1 col1" >orange</td>
<td id="T_809fc9a2_2634_11ea_9473_edbe091121fcrow1_col2" class="data row1 col2" >47</td>
<td id="T_809fc9a2_2634_11ea_9473_edbe091121fcrow1_col3" class="data row1 col3" >10</td>
</tr>
<tr>
<th id="T_809fc9a2_2634_11ea_9473_edbe091121fclevel0_row2" class="row_heading level0 row2" >2</th>
<td id="T_809fc9a2_2634_11ea_9473_edbe091121fcrow2_col0" class="data row2 col0" >Apple</td>
<td id="T_809fc9a2_2634_11ea_9473_edbe091121fcrow2_col1" class="data row2 col1" >red</td>
<td id="T_809fc9a2_2634_11ea_9473_edbe091121fcrow2_col2" class="data row2 col2" >52</td>
<td id="T_809fc9a2_2634_11ea_9473_edbe091121fcrow2_col3" class="data row2 col3" >9</td>
</tr>
<tr>
<th id="T_809fc9a2_2634_11ea_9473_edbe091121fclevel0_row3" class="row_heading level0 row3" >3</th>
<td id="T_809fc9a2_2634_11ea_9473_edbe091121fcrow3_col0" class="data row3 col0" >lemon</td>
<td id="T_809fc9a2_2634_11ea_9473_edbe091121fcrow3_col1" class="data row3 col1" >yellow</td>
<td id="T_809fc9a2_2634_11ea_9473_edbe091121fcrow3_col2" class="data row3 col2" >15</td>
<td id="T_809fc9a2_2634_11ea_9473_edbe091121fcrow3_col3" class="data row3 col3" >7</td>
</tr>
<tr>
<th id="T_809fc9a2_2634_11ea_9473_edbe091121fclevel0_row4" class="row_heading level0 row4" >4</th>
<td id="T_809fc9a2_2634_11ea_9473_edbe091121fcrow4_col0" class="data row4 col0" >lime</td>
<td id="T_809fc9a2_2634_11ea_9473_edbe091121fcrow4_col1" class="data row4 col1" >green</td>
<td id="T_809fc9a2_2634_11ea_9473_edbe091121fcrow4_col2" class="data row4 col2" >30</td>
<td id="T_809fc9a2_2634_11ea_9473_edbe091121fcrow4_col3" class="data row4 col3" >6</td>
</tr>
<tr>
<th id="T_809fc9a2_2634_11ea_9473_edbe091121fclevel0_row5" class="row_heading level0 row5" >5</th>
<td id="T_809fc9a2_2634_11ea_9473_edbe091121fcrow5_col0" class="data row5 col0" >plum</td>
<td id="T_809fc9a2_2634_11ea_9473_edbe091121fcrow5_col1" class="data row5 col1" >purple</td>
<td id="T_809fc9a2_2634_11ea_9473_edbe091121fcrow5_col2" class="data row5 col2" >28</td>
<td id="T_809fc9a2_2634_11ea_9473_edbe091121fcrow5_col3" class="data row5 col3" >4</td>
</tr>
</tbody></table>
<div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">numpy</span> <span class="k">as</span> <span class="nn">np</span>
<span class="n">df</span><span class="p">[</span><span class="s2">&quot;random&quot;</span><span class="p">]</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">random</span><span class="o">.</span><span class="n">rand</span><span class="p">(</span><span class="mi">6</span><span class="p">)</span><span class="o">-</span><span class="mf">0.5</span>
<span class="p">(</span><span class="n">df</span><span class="o">.</span><span class="n">style</span>
<span class="o">.</span><span class="n">bar</span><span class="p">(</span><span class="n">subset</span><span class="o">=</span><span class="p">[</span><span class="s1">&#39;kcal&#39;</span><span class="p">,</span> <span class="s1">&#39;size_cm&#39;</span><span class="p">],</span><span class="n">color</span><span class="o">=</span><span class="s1">&#39;steelblue&#39;</span><span class="p">)</span>
<span class="o">.</span><span class="n">bar</span><span class="p">(</span><span class="n">subset</span><span class="o">=</span><span class="p">[</span><span class="s1">&#39;random&#39;</span><span class="p">],</span><span class="n">color</span><span class="o">=</span><span class="p">[</span><span class="s1">&#39;indianred&#39;</span><span class="p">,</span><span class="s1">&#39;limegreen&#39;</span><span class="p">],</span> <span class="n">align</span><span class="o">=</span><span class="s1">&#39;mid&#39;</span><span class="p">)</span>
<span class="p">)</span>
</pre></div>
<style type="text/css" >
#T_ccd0f210_2634_11ea_9473_edbe091121fcrow0_col2 {
width: 10em;
height: 80%;
background: linear-gradient(90deg,steelblue 100.0%, transparent 100.0%);
} #T_ccd0f210_2634_11ea_9473_edbe091121fcrow0_col3 {
width: 10em;
height: 80%;
background: linear-gradient(90deg,steelblue 100.0%, transparent 100.0%);
} #T_ccd0f210_2634_11ea_9473_edbe091121fcrow0_col4 {
width: 10em;
height: 80%;
background: linear-gradient(90deg, transparent 38.4%, limegreen 38.4%, limegreen 97.3%, transparent 97.3%);
} #T_ccd0f210_2634_11ea_9473_edbe091121fcrow1_col2 {
width: 10em;
height: 80%;
background: linear-gradient(90deg,steelblue 43.2%, transparent 43.2%);
} #T_ccd0f210_2634_11ea_9473_edbe091121fcrow1_col3 {
width: 10em;
height: 80%;
background: linear-gradient(90deg,steelblue 37.5%, transparent 37.5%);
} #T_ccd0f210_2634_11ea_9473_edbe091121fcrow1_col4 {
width: 10em;
height: 80%;
background: linear-gradient(90deg, transparent 38.4%, limegreen 38.4%, limegreen 52.5%, transparent 52.5%);
} #T_ccd0f210_2634_11ea_9473_edbe091121fcrow2_col2 {
width: 10em;
height: 80%;
background: linear-gradient(90deg,steelblue 50.0%, transparent 50.0%);
} #T_ccd0f210_2634_11ea_9473_edbe091121fcrow2_col3 {
width: 10em;
height: 80%;
background: linear-gradient(90deg,steelblue 31.2%, transparent 31.2%);
} #T_ccd0f210_2634_11ea_9473_edbe091121fcrow2_col4 {
width: 10em;
height: 80%;
background: linear-gradient(90deg, transparent 38.4%, limegreen 38.4%, limegreen 53.1%, transparent 53.1%);
} #T_ccd0f210_2634_11ea_9473_edbe091121fcrow3_col2 {
width: 10em;
height: 80%;
} #T_ccd0f210_2634_11ea_9473_edbe091121fcrow3_col3 {
width: 10em;
height: 80%;
background: linear-gradient(90deg,steelblue 18.7%, transparent 18.7%);
} #T_ccd0f210_2634_11ea_9473_edbe091121fcrow3_col4 {
width: 10em;
height: 80%;
background: linear-gradient(90deg,indianred 38.4%, transparent 38.4%);
} #T_ccd0f210_2634_11ea_9473_edbe091121fcrow4_col2 {
width: 10em;
height: 80%;
background: linear-gradient(90deg,steelblue 20.3%, transparent 20.3%);
} #T_ccd0f210_2634_11ea_9473_edbe091121fcrow4_col3 {
width: 10em;
height: 80%;
background: linear-gradient(90deg,steelblue 12.5%, transparent 12.5%);
} #T_ccd0f210_2634_11ea_9473_edbe091121fcrow4_col4 {
width: 10em;
height: 80%;
background: linear-gradient(90deg, transparent 21.6%, indianred 21.6%, indianred 38.4%, transparent 38.4%);
} #T_ccd0f210_2634_11ea_9473_edbe091121fcrow5_col2 {
width: 10em;
height: 80%;
background: linear-gradient(90deg,steelblue 17.6%, transparent 17.6%);
} #T_ccd0f210_2634_11ea_9473_edbe091121fcrow5_col3 {
width: 10em;
height: 80%;
} #T_ccd0f210_2634_11ea_9473_edbe091121fcrow5_col4 {
width: 10em;
height: 80%;
background: linear-gradient(90deg, transparent 38.4%, limegreen 38.4%, limegreen 100.0%, transparent 100.0%);
}</style>
<table id="T_ccd0f210_2634_11ea_9473_edbe091121fc" ><thead> <tr> <th class="blank level0" ></th> <th class="col_heading level0 col0" >fruit</th> <th class="col_heading level0 col1" >color</th> <th class="col_heading level0 col2" >kcal</th> <th class="col_heading level0 col3" >size_cm</th> <th class="col_heading level0 col4" >random</th> </tr></thead><tbody>
<tr>
<th id="T_ccd0f210_2634_11ea_9473_edbe091121fclevel0_row0" class="row_heading level0 row0" >0</th>
<td id="T_ccd0f210_2634_11ea_9473_edbe091121fcrow0_col0" class="data row0 col0" >Banana</td>
<td id="T_ccd0f210_2634_11ea_9473_edbe091121fcrow0_col1" class="data row0 col1" >yellow</td>
<td id="T_ccd0f210_2634_11ea_9473_edbe091121fcrow0_col2" class="data row0 col2" >89</td>
<td id="T_ccd0f210_2634_11ea_9473_edbe091121fcrow0_col3" class="data row0 col3" >20</td>
<td id="T_ccd0f210_2634_11ea_9473_edbe091121fcrow0_col4" class="data row0 col4" >0.432301</td>
</tr>
<tr>
<th id="T_ccd0f210_2634_11ea_9473_edbe091121fclevel0_row1" class="row_heading level0 row1" >1</th>
<td id="T_ccd0f210_2634_11ea_9473_edbe091121fcrow1_col0" class="data row1 col0" >Orange</td>
<td id="T_ccd0f210_2634_11ea_9473_edbe091121fcrow1_col1" class="data row1 col1" >orange</td>
<td id="T_ccd0f210_2634_11ea_9473_edbe091121fcrow1_col2" class="data row1 col2" >47</td>
<td id="T_ccd0f210_2634_11ea_9473_edbe091121fcrow1_col3" class="data row1 col3" >10</td>
<td id="T_ccd0f210_2634_11ea_9473_edbe091121fcrow1_col4" class="data row1 col4" >0.104051</td>
</tr>
<tr>
<th id="T_ccd0f210_2634_11ea_9473_edbe091121fclevel0_row2" class="row_heading level0 row2" >2</th>
<td id="T_ccd0f210_2634_11ea_9473_edbe091121fcrow2_col0" class="data row2 col0" >Apple</td>
<td id="T_ccd0f210_2634_11ea_9473_edbe091121fcrow2_col1" class="data row2 col1" >red</td>
<td id="T_ccd0f210_2634_11ea_9473_edbe091121fcrow2_col2" class="data row2 col2" >52</td>
<td id="T_ccd0f210_2634_11ea_9473_edbe091121fcrow2_col3" class="data row2 col3" >9</td>
<td id="T_ccd0f210_2634_11ea_9473_edbe091121fcrow2_col4" class="data row2 col4" >0.107809</td>
</tr>
<tr>
<th id="T_ccd0f210_2634_11ea_9473_edbe091121fclevel0_row3" class="row_heading level0 row3" >3</th>
<td id="T_ccd0f210_2634_11ea_9473_edbe091121fcrow3_col0" class="data row3 col0" >lemon</td>
<td id="T_ccd0f210_2634_11ea_9473_edbe091121fcrow3_col1" class="data row3 col1" >yellow</td>
<td id="T_ccd0f210_2634_11ea_9473_edbe091121fcrow3_col2" class="data row3 col2" >15</td>
<td id="T_ccd0f210_2634_11ea_9473_edbe091121fcrow3_col3" class="data row3 col3" >7</td>
<td id="T_ccd0f210_2634_11ea_9473_edbe091121fcrow3_col4" class="data row3 col4" >-0.281298</td>
</tr>
<tr>
<th id="T_ccd0f210_2634_11ea_9473_edbe091121fclevel0_row4" class="row_heading level0 row4" >4</th>
<td id="T_ccd0f210_2634_11ea_9473_edbe091121fcrow4_col0" class="data row4 col0" >lime</td>
<td id="T_ccd0f210_2634_11ea_9473_edbe091121fcrow4_col1" class="data row4 col1" >green</td>
<td id="T_ccd0f210_2634_11ea_9473_edbe091121fcrow4_col2" class="data row4 col2" >30</td>
<td id="T_ccd0f210_2634_11ea_9473_edbe091121fcrow4_col3" class="data row4 col3" >6</td>
<td id="T_ccd0f210_2634_11ea_9473_edbe091121fcrow4_col4" class="data row4 col4" >-0.122744</td>
</tr>
<tr>
<th id="T_ccd0f210_2634_11ea_9473_edbe091121fclevel0_row5" class="row_heading level0 row5" >5</th>
<td id="T_ccd0f210_2634_11ea_9473_edbe091121fcrow5_col0" class="data row5 col0" >plum</td>
<td id="T_ccd0f210_2634_11ea_9473_edbe091121fcrow5_col1" class="data row5 col1" >purple</td>
<td id="T_ccd0f210_2634_11ea_9473_edbe091121fcrow5_col2" class="data row5 col2" >28</td>
<td id="T_ccd0f210_2634_11ea_9473_edbe091121fcrow5_col3" class="data row5 col3" >4</td>
<td id="T_ccd0f210_2634_11ea_9473_edbe091121fcrow5_col4" class="data row5 col4" >0.452038</td>
</tr>
</tbody></table>
</div>
<aside>
<div class="bug-reporting__panel">
<h3>Find an error or bug? Have a suggestion?</h3>
<p>Everything on this site is avaliable on GitHub. Head on over and <a href='https://github.com/redoules/redoules.github.io/issues/new'>submit an issue.</a> You can also message me directly by <a href='mailto:guillaume.redoules@gadz.org'>email</a>.</p>
</div>
</aside>
</section>
</div>
<!-- start of footer section -->
<footer class="footer">
<div class="container">
<p class="text-muted">
<center>This project contains 119 pages and is available on <a href="https://github.com/redoules/redoules.github.io">GitHub</a>.
<br/>
Copyright &copy; Guillaume Redoulès,
<time datetime="2018">2018</time>.
</center>
</p>
</div>
</footer>
<!-- This jQuery line finds any span that contains code highlighting classes and then selects the parent <pre> tag and adds a border. This is done as a workaround to visually distinguish the code inputs and outputs -->
<script>
$( ".hll, .n, .c, .err, .k, .o, .cm, .cp, .c1, .cs, .gd, .ge, .gr, .gh, .gi, .go, .gp, .gs, .gu, .gt, .kc, .kd, .kn, .kp, .kr, .kt, .m, .s, .na, .nb, .nc, .no, .nd, .ni, .ne, .nf, .nl, .nn, .nt, .nv, .ow, .w, .mf, .mh, .mi, .mo, .sb, .sc, .sd, .s2, .se, .sh, .si, .sx, .sr, .s1, .ss, .bp, .vc, .vg, .vi, .il" ).parent( "pre" ).css( "border", "1px solid #DEDEDE" );
</script>
<!-- Load Google Analytics -->
<script>
/*
(function(i, s, o, g, r, a, m) {
i['GoogleAnalyticsObject'] = r;
i[r] = i[r] || function() {
(i[r].q = i[r].q || []).push(arguments)
}, i[r].l = 1 * new Date();
a = s.createElement(o),
m = s.getElementsByTagName(o)[0];
a.async = 1;
a.src = g;
m.parentNode.insertBefore(a, m)
})(window, document, 'script', '//www.google-analytics.com/analytics.js', 'ga');
ga('create', 'UA-66582-32', 'auto');
ga('send', 'pageview');
*/
</script>
<!-- End of Google Analytics -->
<!-- Bootstrap core JavaScript
================================================== -->
<!-- Placed at the end of the document so the pages load faster -->
<script src="../theme/js/bootstrap.min.js"></script>
<!-- IE10 viewport hack for Surface/desktop Windows 8 bug -->
<script src="../theme/js/ie10-viewport-bug-workaround.js"></script>
</body>
</html>