{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"id": "french-checklist",
"metadata": {},
"outputs": [],
"source": [
"import pandas as pd"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "otherwise-bride",
"metadata": {},
"outputs": [],
"source": [
"# 샘플데이터 파일 생성: %%writefile"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "hourly-recipe",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Writing sample1.csv\n"
]
}
],
"source": [
"%%writefile sample1.csv\n",
"#현재 디렉토리(파이썬, 주피터가 실행된 디렉토리)에 아래 내용의 sample1.csv 생성\n",
"\n",
"c1, c2, c3\n",
"1, 1.11, one\n",
"2, 2.22, two\n",
"3, 3.33, three"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "improving-comment",
"metadata": {},
"outputs": [],
"source": [
"# CSV 파일 불러오기"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "romantic-combination",
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"
\n",
"\n",
"
\n",
" \n",
" \n",
" | \n",
" #현재 디렉토리(파이썬 | \n",
" 주피터가 실행된 디렉토리)에 아래 내용의 sample1.csv 생성 | \n",
"
\n",
" \n",
" \n",
" \n",
" c1 | \n",
" c2 | \n",
" c3 | \n",
"
\n",
" \n",
" 1 | \n",
" 1.11 | \n",
" one | \n",
"
\n",
" \n",
" 2 | \n",
" 2.22 | \n",
" two | \n",
"
\n",
" \n",
" 3 | \n",
" 3.33 | \n",
" three | \n",
"
\n",
" \n",
"
\n",
"
"
],
"text/plain": [
" #현재 디렉토리(파이썬 주피터가 실행된 디렉토리)에 아래 내용의 sample1.csv 생성\n",
"c1 c2 c3\n",
"1 1.11 one\n",
"2 2.22 two\n",
"3 3.33 three"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"pd.read_csv('sample1.csv')"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "stuffed-revision",
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"\n",
"\n",
"
\n",
" \n",
" \n",
" | \n",
" 5.1 | \n",
" 3.5 | \n",
" 1.4 | \n",
" 0.2 | \n",
" 1 | \n",
"
\n",
" \n",
" \n",
" \n",
" 0 | \n",
" 4.9 | \n",
" 3.0 | \n",
" 1.4 | \n",
" 0.2 | \n",
" 1 | \n",
"
\n",
" \n",
" 1 | \n",
" 4.7 | \n",
" 3.2 | \n",
" 1.3 | \n",
" 0.2 | \n",
" 1 | \n",
"
\n",
" \n",
" 2 | \n",
" 4.6 | \n",
" 3.1 | \n",
" 1.5 | \n",
" 0.2 | \n",
" 1 | \n",
"
\n",
" \n",
" 3 | \n",
" 5.0 | \n",
" 3.6 | \n",
" 1.4 | \n",
" 0.2 | \n",
" 1 | \n",
"
\n",
" \n",
" 4 | \n",
" 5.4 | \n",
" 3.9 | \n",
" 1.7 | \n",
" 0.4 | \n",
" 1 | \n",
"
\n",
" \n",
" ... | \n",
" ... | \n",
" ... | \n",
" ... | \n",
" ... | \n",
" ... | \n",
"
\n",
" \n",
" 69 | \n",
" 6.9 | \n",
" 3.2 | \n",
" 5.7 | \n",
" 2.3 | \n",
" 3 | \n",
"
\n",
" \n",
" 70 | \n",
" 5.6 | \n",
" 2.8 | \n",
" 4.9 | \n",
" 2.0 | \n",
" 3 | \n",
"
\n",
" \n",
" 71 | \n",
" 7.7 | \n",
" 2.8 | \n",
" 6.7 | \n",
" 2.0 | \n",
" 3 | \n",
"
\n",
" \n",
" 72 | \n",
" 6.3 | \n",
" 2.7 | \n",
" 4.9 | \n",
" 1.8 | \n",
" 3 | \n",
"
\n",
" \n",
" 73 | \n",
" 6.7 | \n",
" 3.3 | \n",
" 5.7 | \n",
" 2.1 | \n",
" 3 | \n",
"
\n",
" \n",
"
\n",
"
74 rows × 5 columns
\n",
"
"
],
"text/plain": [
" 5.1 3.5 1.4 0.2 1\n",
"0 4.9 3.0 1.4 0.2 1\n",
"1 4.7 3.2 1.3 0.2 1\n",
"2 4.6 3.1 1.5 0.2 1\n",
"3 5.0 3.6 1.4 0.2 1\n",
"4 5.4 3.9 1.7 0.4 1\n",
".. ... ... ... ... ..\n",
"69 6.9 3.2 5.7 2.3 3\n",
"70 5.6 2.8 4.9 2.0 3\n",
"71 7.7 2.8 6.7 2.0 3\n",
"72 6.3 2.7 4.9 1.8 3\n",
"73 6.7 3.3 5.7 2.1 3\n",
"\n",
"[74 rows x 5 columns]"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"pd.read_csv('c:/sampledata/iris.csv')"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "prospective-broadcasting",
"metadata": {},
"outputs": [],
"source": [
"# 만약 데이터 파일에 열 인덱스 정보가 없는 경우에는 read_csv 명령의 names 인수로 설정할 수 있다."
]
},
{
"cell_type": "code",
"execution_count": 8,
"id": "rolled-comparative",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Writing sample2.csv\n"
]
}
],
"source": [
"%%writefile sample2.csv\n",
"1, 1.11, one\n",
"2, 2.22, two\n",
"3, 3.33, three"
]
},
{
"cell_type": "code",
"execution_count": 9,
"id": "short-surge",
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"\n",
"\n",
"
\n",
" \n",
" \n",
" | \n",
" c1 | \n",
" c2 | \n",
" c3 | \n",
"
\n",
" \n",
" \n",
" \n",
" 0 | \n",
" 1 | \n",
" 1.11 | \n",
" one | \n",
"
\n",
" \n",
" 1 | \n",
" 2 | \n",
" 2.22 | \n",
" two | \n",
"
\n",
" \n",
" 2 | \n",
" 3 | \n",
" 3.33 | \n",
" three | \n",
"
\n",
" \n",
"
\n",
"
"
],
"text/plain": [
" c1 c2 c3\n",
"0 1 1.11 one\n",
"1 2 2.22 two\n",
"2 3 3.33 three"
]
},
"execution_count": 9,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"pd.read_csv('sample2.csv', names=['c1', 'c2', 'c3'])"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "likely-abraham",
"metadata": {},
"outputs": [],
"source": [
"# 특정한 열(column)을 행(row) 인덱스로 지정하여 불러오기"
]
},
{
"cell_type": "code",
"execution_count": 21,
"id": "fluid-vehicle",
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"\n",
"\n",
"
\n",
" \n",
" \n",
" | \n",
" #현재 디렉토리(파이썬 | \n",
" 주피터가 실행된 디렉토리)에 아래 내용의 sample1.csv 생성 | \n",
"
\n",
" \n",
" \n",
" \n",
" c1 | \n",
" c2 | \n",
" c3 | \n",
"
\n",
" \n",
" 1 | \n",
" 1.11 | \n",
" one | \n",
"
\n",
" \n",
" 2 | \n",
" 2.22 | \n",
" two | \n",
"
\n",
" \n",
" 3 | \n",
" 3.33 | \n",
" three | \n",
"
\n",
" \n",
"
\n",
"
"
],
"text/plain": [
" #현재 디렉토리(파이썬 주피터가 실행된 디렉토리)에 아래 내용의 sample1.csv 생성\n",
"c1 c2 c3\n",
"1 1.11 one\n",
"2 2.22 two\n",
"3 3.33 three"
]
},
"execution_count": 21,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"pd.read_csv('sample1.csv', index_col = 0) # index_col = 위치(숫자), 'ID'"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "dietary-toilet",
"metadata": {},
"outputs": [],
"source": [
"# 데이터간 구분자(separator)가 쉼표(comma)가 아닌 파일의 입출력"
]
},
{
"cell_type": "code",
"execution_count": 22,
"id": "generic-relaxation",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Writing sample3.txt\n"
]
}
],
"source": [
"%%writefile sample3.txt\n",
"c1 c2 c3 c4\n",
"0.179181 -1.538472 1.347553 0.43381\n",
"1.024209 0.087307 -1.281997 0.49265\n",
"0.417899 -2.002308 0.255245 -1.10515"
]
},
{
"cell_type": "code",
"execution_count": 23,
"id": "floral-estimate",
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"\n",
"\n",
"
\n",
" \n",
" \n",
" | \n",
" c1 | \n",
" c2 | \n",
" c3 | \n",
" c4 | \n",
"
\n",
" \n",
" \n",
" \n",
" 0 | \n",
" 0.179181 | \n",
" -1.538472 | \n",
" 1.347553 | \n",
" 0.43381 | \n",
"
\n",
" \n",
" 1 | \n",
" 1.024209 | \n",
" 0.087307 | \n",
" -1.281997 | \n",
" 0.49265 | \n",
"
\n",
" \n",
" 2 | \n",
" 0.417899 | \n",
" -2.002308 | \n",
" 0.255245 | \n",
" -1.10515 | \n",
"
\n",
" \n",
"
\n",
"
"
],
"text/plain": [
" c1 c2 c3 c4\n",
"0 0.179181 -1.538472 1.347553 0.43381\n",
"1 1.024209 0.087307 -1.281997 0.49265\n",
"2 0.417899 -2.002308 0.255245 -1.10515"
]
},
"execution_count": 23,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"pd.read_table('sample3.txt', sep='\\s+') #sep 또는 \\s+ 사용"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "explicit-consideration",
"metadata": {},
"outputs": [],
"source": [
"# 파일 중 제목행과 같이 건너 뛰어야 할 행이 있는 경우: skiprows"
]
},
{
"cell_type": "code",
"execution_count": 24,
"id": "dimensional-attitude",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Writing sample4.txt\n"
]
}
],
"source": [
"%%writefile sample4.txt\n",
"파일 제목: sample4.txt\n",
"데이터 포맷의 설명:\n",
"c1, c2, c3\n",
"1, 1.11, one\n",
"2, 2.22, two\n",
"3, 3.33, three"
]
},
{
"cell_type": "code",
"execution_count": 25,
"id": "weekly-civilization",
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"\n",
"\n",
"
\n",
" \n",
" \n",
" | \n",
" c1 | \n",
" c2 | \n",
" c3 | \n",
"
\n",
" \n",
" \n",
" \n",
" 0 | \n",
" 1 | \n",
" 1.11 | \n",
" one | \n",
"
\n",
" \n",
" 1 | \n",
" 2 | \n",
" 2.22 | \n",
" two | \n",
"
\n",
" \n",
" 2 | \n",
" 3 | \n",
" 3.33 | \n",
" three | \n",
"
\n",
" \n",
"
\n",
"
"
],
"text/plain": [
" c1 c2 c3\n",
"0 1 1.11 one\n",
"1 2 2.22 two\n",
"2 3 3.33 three"
]
},
"execution_count": 25,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"pd.read_csv('sample4.txt', skiprows = [0, 1])"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "conventional-exhaust",
"metadata": {},
"outputs": [],
"source": [
"# 특정한 값을 NaN으로 바꿀 때: na_values"
]
},
{
"cell_type": "code",
"execution_count": 26,
"id": "satellite-wallpaper",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Writing sample5.csv\n"
]
}
],
"source": [
"%%writefile sample5.csv\n",
"c1, c2, c3\n",
"1, 1.11, one\n",
"2, , two\n",
"누락, 3.33, three"
]
},
{
"cell_type": "code",
"execution_count": 27,
"id": "characteristic-stuart",
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"\n",
"\n",
"
\n",
" \n",
" \n",
" | \n",
" c1 | \n",
" c2 | \n",
" c3 | \n",
"
\n",
" \n",
" \n",
" \n",
" 0 | \n",
" 1.0 | \n",
" 1.11 | \n",
" one | \n",
"
\n",
" \n",
" 1 | \n",
" 2.0 | \n",
" | \n",
" two | \n",
"
\n",
" \n",
" 2 | \n",
" NaN | \n",
" 3.33 | \n",
" three | \n",
"
\n",
" \n",
"
\n",
"
"
],
"text/plain": [
" c1 c2 c3\n",
"0 1.0 1.11 one\n",
"1 2.0 two\n",
"2 NaN 3.33 three"
]
},
"execution_count": 27,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df = pd.read_csv('sample5.csv', na_values = ['누락'])\n",
"df"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "young-allah",
"metadata": {},
"outputs": [],
"source": [
"# 데이터프레임 to CSV"
]
},
{
"cell_type": "code",
"execution_count": 28,
"id": "worldwide-defeat",
"metadata": {},
"outputs": [],
"source": [
"df.to_csv('sample6.csv')"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "union-repository",
"metadata": {},
"outputs": [],
"source": [
"# 파일 내용 확인"
]
},
{
"cell_type": "code",
"execution_count": 30,
"id": "accompanied-indicator",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
",c1, c2, c3\n",
"0,1.0, 1.11, one\n",
"1,2.0, , two\n",
"2,, 3.33, three\n"
]
}
],
"source": [
"!type sample6.csv"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "alone-trash",
"metadata": {},
"outputs": [],
"source": [
"# 인터넷 상의 CSV 파일 가져오기"
]
},
{
"cell_type": "code",
"execution_count": 31,
"id": "center-kitty",
"metadata": {},
"outputs": [],
"source": [
"df = pd.read_csv(\"https://raw.githubusercontent.com/datascienceschool/docker_rpython/master/data/titanic.csv\")"
]
},
{
"cell_type": "code",
"execution_count": 32,
"id": "stunning-construction",
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"\n",
"\n",
"
\n",
" \n",
" \n",
" | \n",
" PassengerId | \n",
" Survived | \n",
" Pclass | \n",
" Name | \n",
" Sex | \n",
" Age | \n",
" SibSp | \n",
" Parch | \n",
" Ticket | \n",
" Fare | \n",
" Cabin | \n",
" Embarked | \n",
"
\n",
" \n",
" \n",
" \n",
" 0 | \n",
" 1 | \n",
" 0 | \n",
" 3 | \n",
" Braund, Mr. Owen Harris | \n",
" male | \n",
" 22.0 | \n",
" 1 | \n",
" 0 | \n",
" A/5 21171 | \n",
" 7.2500 | \n",
" NaN | \n",
" S | \n",
"
\n",
" \n",
" 1 | \n",
" 2 | \n",
" 1 | \n",
" 1 | \n",
" Cumings, Mrs. John Bradley (Florence Briggs Th... | \n",
" female | \n",
" 38.0 | \n",
" 1 | \n",
" 0 | \n",
" PC 17599 | \n",
" 71.2833 | \n",
" C85 | \n",
" C | \n",
"
\n",
" \n",
"
\n",
"
"
],
"text/plain": [
" PassengerId Survived Pclass \\\n",
"0 1 0 3 \n",
"1 2 1 1 \n",
"\n",
" Name Sex Age SibSp \\\n",
"0 Braund, Mr. Owen Harris male 22.0 1 \n",
"1 Cumings, Mrs. John Bradley (Florence Briggs Th... female 38.0 1 \n",
"\n",
" Parch Ticket Fare Cabin Embarked \n",
"0 0 A/5 21171 7.2500 NaN S \n",
"1 0 PC 17599 71.2833 C85 C "
]
},
"execution_count": 32,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df[:2]"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "bacterial-bradley",
"metadata": {},
"outputs": [],
"source": [
"# 인터넷 상의 데이터베이스 입력"
]
},
{
"cell_type": "code",
"execution_count": 33,
"id": "compound-istanbul",
"metadata": {},
"outputs": [],
"source": [
"import datetime\n",
"\n",
"dt_start = datetime.datetime(2015, 1, 1)\n",
"dt_end = '2016, 6, 30'"
]
},
{
"cell_type": "code",
"execution_count": 35,
"id": "american-inspector",
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"\n",
"\n",
"
\n",
" \n",
" \n",
" | \n",
" GDP | \n",
"
\n",
" \n",
" DATE | \n",
" | \n",
"
\n",
" \n",
" \n",
" \n",
" 2015-04-01 | \n",
" 18223.577 | \n",
"
\n",
" \n",
" 2015-07-01 | \n",
" 18347.425 | \n",
"
\n",
" \n",
" 2015-10-01 | \n",
" 18378.803 | \n",
"
\n",
" \n",
" 2016-01-01 | \n",
" 18470.156 | \n",
"
\n",
" \n",
" 2016-04-01 | \n",
" 18656.207 | \n",
"
\n",
" \n",
"
\n",
"
"
],
"text/plain": [
" GDP\n",
"DATE \n",
"2015-04-01 18223.577\n",
"2015-07-01 18347.425\n",
"2015-10-01 18378.803\n",
"2016-01-01 18470.156\n",
"2016-04-01 18656.207"
]
},
"execution_count": 35,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"import pandas_datareader as pdr #pip install pandas-datareader\n",
"\n",
"gdp = pdr.get_data_fred('GDP', dt_start, dt_end)\n",
"gdp.tail()"
]
},
{
"cell_type": "code",
"execution_count": 36,
"id": "organizational-dinner",
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"\n",
"\n",
"
\n",
" \n",
" \n",
" | \n",
" CPIAUCSL | \n",
" CPILFESL | \n",
"
\n",
" \n",
" DATE | \n",
" | \n",
" | \n",
"
\n",
" \n",
" \n",
" \n",
" 2016-02-01 | \n",
" 237.336 | \n",
" 245.510 | \n",
"
\n",
" \n",
" 2016-03-01 | \n",
" 238.080 | \n",
" 245.913 | \n",
"
\n",
" \n",
" 2016-04-01 | \n",
" 238.992 | \n",
" 246.551 | \n",
"
\n",
" \n",
" 2016-05-01 | \n",
" 239.557 | \n",
" 247.137 | \n",
"
\n",
" \n",
" 2016-06-01 | \n",
" 240.222 | \n",
" 247.540 | \n",
"
\n",
" \n",
"
\n",
"
"
],
"text/plain": [
" CPIAUCSL CPILFESL\n",
"DATE \n",
"2016-02-01 237.336 245.510\n",
"2016-03-01 238.080 245.913\n",
"2016-04-01 238.992 246.551\n",
"2016-05-01 239.557 247.137\n",
"2016-06-01 240.222 247.540"
]
},
"execution_count": 36,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"inflation = pdr.get_data_fred([\"CPIAUCSL\", \"CPILFESL\"], dt_start, dt_end) #데이터 리스트 추가\n",
"inflation.tail()"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "filled-intelligence",
"metadata": {},
"outputs": [],
"source": []
}
],
"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.8.8"
}
},
"nbformat": 4,
"nbformat_minor": 5
}