{
"cells": [
{
"cell_type": "markdown",
"metadata": {
"collapsed": true
},
"source": [
"# FAQ - pandas 출력 포맷팅\n",
"* Q: pandas 포맷팅 - 숫자에 e가 표시여부, 소수점 자리수 등을 지정하여 읽기 쉽게 하고자 합니다\n",
"* A: pd.set_option()으로 상세한 조정이 가능합니다\n",
"\n",
"#### 2017 https://financedata.github.io\n",
""
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"import pandas as pd\n",
"import numpy as np"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"
\n",
"
\n",
" \n",
" \n",
" | \n",
" A | \n",
"
\n",
" \n",
" \n",
" \n",
" 0 | \n",
" 6624.065033 | \n",
"
\n",
" \n",
" 1 | \n",
" 5576.084574 | \n",
"
\n",
" \n",
" 2 | \n",
" 5984.188094 | \n",
"
\n",
" \n",
" 3 | \n",
" 6306.194602 | \n",
"
\n",
" \n",
" 4 | \n",
" 8533.817736 | \n",
"
\n",
" \n",
"
\n",
"
"
],
"text/plain": [
" A\n",
"0 6624.065033\n",
"1 5576.084574\n",
"2 5984.188094\n",
"3 6306.194602\n",
"4 8533.817736"
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df = pd.DataFrame()\n",
"df['A'] = np.random.rand(5) * 10000\n",
"df"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"숫자 표현에 e는 10 승수를 표현합니다. 이런 표기법을 과학적 표기법(Scientific notation)이라고 하며 아주 큰 수나, 아주 작을 수를 표현하는데 효과적이다. \n",
"\n",
"pandas.option 객체 pandas.set_option() 함수를 사용하여 출력 형태를 다양하게 지정할 수 있다. 예를 들어, 소수점 이하 2자리만 표현하고 싶다면 아래와 같이 할 수 있다. 이때, f 혹은 e 를 사용하여 과학적 표기법을 쓸지 여부를 지정할 수 있다."
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"\n",
"
\n",
" \n",
" \n",
" | \n",
" A | \n",
"
\n",
" \n",
" \n",
" \n",
" 0 | \n",
" 6624.07 | \n",
"
\n",
" \n",
" 1 | \n",
" 5576.08 | \n",
"
\n",
" \n",
" 2 | \n",
" 5984.19 | \n",
"
\n",
" \n",
" 3 | \n",
" 6306.19 | \n",
"
\n",
" \n",
" 4 | \n",
" 8533.82 | \n",
"
\n",
" \n",
"
\n",
"
"
],
"text/plain": [
" A\n",
"0 6624.07\n",
"1 5576.08\n",
"2 5984.19\n",
"3 6306.19\n",
"4 8533.82"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# 과학적 표기법(Scientific notation)을 사용하지 않는 경우\n",
"\n",
"pd.options.display.float_format = '{:.2f}'.format\n",
"df"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"\n",
"
\n",
" \n",
" \n",
" | \n",
" A | \n",
"
\n",
" \n",
" \n",
" \n",
" 0 | \n",
" 6.62e+03 | \n",
"
\n",
" \n",
" 1 | \n",
" 5.58e+03 | \n",
"
\n",
" \n",
" 2 | \n",
" 5.98e+03 | \n",
"
\n",
" \n",
" 3 | \n",
" 6.31e+03 | \n",
"
\n",
" \n",
" 4 | \n",
" 8.53e+03 | \n",
"
\n",
" \n",
"
\n",
"
"
],
"text/plain": [
" A\n",
"0 6.62e+03\n",
"1 5.58e+03\n",
"2 5.98e+03\n",
"3 6.31e+03\n",
"4 8.53e+03"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# 과학적 표기법(Scientific notation)\n",
"\n",
"pd.set_option('display.float_format', '{:.2e}'.format)\n",
"df"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"pd.set_option('display.float_format', '{:.2f}'.format) # 항상 float 형식으로\n",
"pd.set_option('display.float_format', '{:.2e}'.format) # 항상 사이언티픽\n",
"pd.set_option('display.float_format', '${:.2g}'.format) # 적당히 알아서"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"지정한 표기법을 원래 상태로 돌리려면 None을 지정한다."
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"\n",
"
\n",
" \n",
" \n",
" | \n",
" A | \n",
"
\n",
" \n",
" \n",
" \n",
" 0 | \n",
" 6624.065033 | \n",
"
\n",
" \n",
" 1 | \n",
" 5576.084574 | \n",
"
\n",
" \n",
" 2 | \n",
" 5984.188094 | \n",
"
\n",
" \n",
" 3 | \n",
" 6306.194602 | \n",
"
\n",
" \n",
" 4 | \n",
" 8533.817736 | \n",
"
\n",
" \n",
"
\n",
"
"
],
"text/plain": [
" A\n",
"0 6624.065033\n",
"1 5576.084574\n",
"2 5984.188094\n",
"3 6306.194602\n",
"4 8533.817736"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"pd.set_option('display.float_format', None)\n",
"df"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# 참고자료\n",
"* http://pandas.pydata.org/pandas-docs/stable/options.html"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### 2017 https://fb.com/financedata"
]
}
],
"metadata": {
"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": "dev",
"date": "2017-05-02 14:45:16 UTC+09:00",
"description": "설명",
"link": "",
"slug": "pandas-display-format",
"tags": "pandas, display, format",
"title": "pandas 출력 포맷팅",
"type": "text"
}
},
"nbformat": 4,
"nbformat_minor": 2
}