Skip to content

Instantly share code, notes, and snippets.

@ksomemo
Last active July 24, 2017 02:31
Show Gist options
  • Save ksomemo/d3d1a47baebcceeafef949895612ec92 to your computer and use it in GitHub Desktop.
Save ksomemo/d3d1a47baebcceeafef949895612ec92 to your computer and use it in GitHub Desktop.
Notebookに複数のDataFrameを(水平に)出力する+displayについて
import pandas as pd
import IPython.core.display as display
import IPython.display
"""
from IPython.core.display import *
from IPython.lib.display import *
lib.display includes
__all__ = ['Audio', 'IFrame', 'YouTubeVideo', 'VimeoVideo', 'ScribdDocument',
'FileLink', 'FileLinks']
"""
df = pd.DataFrame(dict(a=range(4)))
print("index: 0 (print)")
IPython.display.display(df[0:1])
print("index: 1 (print)")
display.display(df[1:2])
print("index: 2 (print)")
display.display_html(df[2:3])
print("index: 3 (Out to Cell)")
df[3:]
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"ExecuteTime": {
"end_time": "2017-01-22T02:53:50.458845",
"start_time": "2017-01-22T02:53:49.441769"
},
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"index: 0 (print)\n"
]
},
{
"data": {
"text/html": [
"<div>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>a</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>0</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" a\n",
"0 0"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"index: 1 (print)\n"
]
},
{
"data": {
"text/html": [
"<div>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>a</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>1</th>\n",
" <td>1</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" a\n",
"1 1"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"index: 2 (print)\n"
]
},
{
"data": {
"text/html": [
"<div>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>a</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>2</th>\n",
" <td>2</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"index: 3 (Out to Cell)\n"
]
},
{
"data": {
"text/html": [
"<div>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>a</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>3</th>\n",
" <td>3</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" a\n",
"3 3"
]
},
"execution_count": 1,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"import pandas as pd\n",
"import IPython.core.display as display\n",
"import IPython.display\n",
"\"\"\"\n",
"↑\n",
"from IPython.core.display import *\n",
"from IPython.lib.display import *\n",
"\n",
"lib.display includes \n",
"__all__ = ['Audio', 'IFrame', 'YouTubeVideo', 'VimeoVideo', 'ScribdDocument',\n",
" 'FileLink', 'FileLinks']\n",
"\"\"\"\n",
"\n",
"df = pd.DataFrame(dict(a=range(4)))\n",
"\n",
"print(\"index: 0 (print)\")\n",
"IPython.display.display(df[0:1])\n",
"\n",
"print(\"index: 1 (print)\")\n",
"display.display(df[1:2])\n",
"\n",
"print(\"index: 2 (print)\")\n",
"display.display_html(df[2:3])\n",
"\n",
"print(\"index: 3 (Out to Cell)\")\n",
"df[3:]"
]
}
],
"metadata": {
"hide_input": false,
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"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.5.1"
},
"toc": {
"toc_cell": false,
"toc_number_sections": true,
"toc_threshold": 6,
"toc_window_display": false
}
},
"nbformat": 4,
"nbformat_minor": 0
}
class HorizontalDisplay:
def __init__(self, *args):
self.args = args
def _repr_html_(self):
template = '<div style="float: left; padding: 10px;">{0}</div>'
return "\n".join(template.format(arg._repr_html_())
for arg in self.args)
# print
display(HorizontalDisplay(df, df))
# output
HorizontalDisplay(df, df)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment