{
"cells": [
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"# [FAQ] Jupyter Notebook에서 값을 연속적으로 출력하려면?\n",
"* Q: Jupyter Notebook 셀에서 마지막 값 하나만이 아니라 중간에 모든 값을 차례로 출력하고 싶습니다.\n",
"* A: print(), IPython.display를 써서 매번 출력하거나 InteractiveShell의 옵션을 지정합니다.\n",
"\n",
"\n",
"\n",
"#### 2017 https://fb.com/financedata , https://financedata.github.io\n",
""
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"Jupyter Notebook에서 항상 마지막 값만 출력된다."
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"slideshow": {
"slide_type": "-"
}
},
"outputs": [
{
"data": {
"text/plain": [
"array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])"
]
},
"execution_count": 1,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"import pandas as pd\n",
"import numpy as np\n",
"\n",
"df = pd.DataFrame(np.random.randn(5, 3), columns=list('ABC'))\n",
"lst = np.arange(10)\n",
"\n",
"df\n",
"lst"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "-"
}
},
"source": [
"모든 출력값을 매번 연속적으로 출력하려면 어떻게 할까? 즉, df, lst 값을 모두 차례로 출력하려면?"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"# 방법 1 - 매번 print()를 사용\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"slideshow": {
"slide_type": "-"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
" A B C\n",
"0 -0.975347 0.785468 -1.242341\n",
"1 0.278320 -2.819004 -1.543017\n",
"2 0.012055 0.549362 0.970234\n",
"3 -0.985156 1.670095 -0.563928\n",
"4 -0.088859 1.582515 0.881899\n",
"[0 1 2 3 4 5 6 7 8 9]\n"
]
}
],
"source": [
"print(df)\n",
"print(lst)"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"# 방법 2 - display로 출력\n",
"\n",
"DataFrame()을 HTML로 출력하기 위해 print() 대신 IPython.display를 사용할 수 있다."
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {
"collapsed": true,
"slideshow": {
"slide_type": "-"
}
},
"outputs": [],
"source": [
"from IPython.display import display"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {
"slideshow": {
"slide_type": "-"
}
},
"outputs": [
{
"data": {
"text/html": [
"
\n",
"\n",
"
\n",
" \n",
" \n",
" | \n",
" A | \n",
" B | \n",
" C | \n",
"
\n",
" \n",
" \n",
" \n",
" 0 | \n",
" -0.975347 | \n",
" 0.785468 | \n",
" -1.242341 | \n",
"
\n",
" \n",
" 1 | \n",
" 0.278320 | \n",
" -2.819004 | \n",
" -1.543017 | \n",
"
\n",
" \n",
" 2 | \n",
" 0.012055 | \n",
" 0.549362 | \n",
" 0.970234 | \n",
"
\n",
" \n",
" 3 | \n",
" -0.985156 | \n",
" 1.670095 | \n",
" -0.563928 | \n",
"
\n",
" \n",
" 4 | \n",
" -0.088859 | \n",
" 1.582515 | \n",
" 0.881899 | \n",
"
\n",
" \n",
"
\n",
"
"
],
"text/plain": [
" A B C\n",
"0 -0.975347 0.785468 -1.242341\n",
"1 0.278320 -2.819004 -1.543017\n",
"2 0.012055 0.549362 0.970234\n",
"3 -0.985156 1.670095 -0.563928\n",
"4 -0.088859 1.582515 0.881899"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/plain": [
"array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"display(df)\n",
"display(lst)"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"# 방법 3 - InteractiveShell의 옵션 지정\n",
"\n",
"InteractiveShell의 옵션을 지정하여 모두 출력되도록 할 수 있다\n",
"\n",
"InteractiveShell.ast_node_interactivity : 'all' | 'last' | 'last_expr' | 'none' (기본값은 'last_expr')"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {
"collapsed": true,
"slideshow": {
"slide_type": "-"
}
},
"outputs": [],
"source": [
"from IPython.core.interactiveshell import InteractiveShell\n",
"InteractiveShell.ast_node_interactivity = \"all\""
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {
"slideshow": {
"slide_type": "-"
}
},
"outputs": [
{
"data": {
"text/html": [
"\n",
"\n",
"
\n",
" \n",
" \n",
" | \n",
" A | \n",
" B | \n",
" C | \n",
"
\n",
" \n",
" \n",
" \n",
" 0 | \n",
" -0.975347 | \n",
" 0.785468 | \n",
" -1.242341 | \n",
"
\n",
" \n",
" 1 | \n",
" 0.278320 | \n",
" -2.819004 | \n",
" -1.543017 | \n",
"
\n",
" \n",
" 2 | \n",
" 0.012055 | \n",
" 0.549362 | \n",
" 0.970234 | \n",
"
\n",
" \n",
" 3 | \n",
" -0.985156 | \n",
" 1.670095 | \n",
" -0.563928 | \n",
"
\n",
" \n",
" 4 | \n",
" -0.088859 | \n",
" 1.582515 | \n",
" 0.881899 | \n",
"
\n",
" \n",
"
\n",
"
"
],
"text/plain": [
" A B C\n",
"0 -0.975347 0.785468 -1.242341\n",
"1 0.278320 -2.819004 -1.543017\n",
"2 0.012055 0.549362 0.970234\n",
"3 -0.985156 1.670095 -0.563928\n",
"4 -0.088859 1.582515 0.881899"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
},
{
"data": {
"text/plain": [
"array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
},
{
"data": {
"text/plain": [
"'hello, world'"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
},
{
"data": {
"text/plain": [
"array([ 0, 1, 4, 9, 16, 25, 36, 49, 64, 81])"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df\n",
"lst\n",
"'hello, world'\n",
"np.arange(10) ** 2"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "notes"
}
},
"source": [
"(참고) http://ipython.readthedocs.io/en/stable/config/options/terminal.html"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "notes"
}
},
"source": [
"#### 2017 https://fb.com/financedata"
]
}
],
"metadata": {
"celltoolbar": "Slideshow",
"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.2"
},
"nikola": {
"category": "faq",
"date": "2017-05-27 10:40:11 UTC+09:00",
"description": "Jupyter Notebook 셀에서 값을 연속적으로 모두 출력하기",
"link": "",
"slug": "display-all-values-in-cell-jupyter-notebook",
"tags": "python, jupyter, notebook",
"title": "Jupyter Notebook 셀에서 값을 연속적으로 모두 출력하려면?",
"type": "text"
}
},
"nbformat": 4,
"nbformat_minor": 2
}