{ "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", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
#현재 디렉토리(파이썬주피터가 실행된 디렉토리)에 아래 내용의 sample1.csv 생성
c1c2c3
11.11one
22.22two
33.33three
\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", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
5.13.51.40.21
04.93.01.40.21
14.73.21.30.21
24.63.11.50.21
35.03.61.40.21
45.43.91.70.41
..................
696.93.25.72.33
705.62.84.92.03
717.72.86.72.03
726.32.74.91.83
736.73.35.72.13
\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", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
c1c2c3
011.11one
122.22two
233.33three
\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", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
#현재 디렉토리(파이썬주피터가 실행된 디렉토리)에 아래 내용의 sample1.csv 생성
c1c2c3
11.11one
22.22two
33.33three
\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", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
c1c2c3c4
00.179181-1.5384721.3475530.43381
11.0242090.087307-1.2819970.49265
20.417899-2.0023080.255245-1.10515
\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", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
c1c2c3
011.11one
122.22two
233.33three
\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", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
c1c2c3
01.01.11one
12.0two
2NaN3.33three
\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", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
PassengerIdSurvivedPclassNameSexAgeSibSpParchTicketFareCabinEmbarked
0103Braund, Mr. Owen Harrismale22.010A/5 211717.2500NaNS
1211Cumings, Mrs. John Bradley (Florence Briggs Th...female38.010PC 1759971.2833C85C
\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", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
GDP
DATE
2015-04-0118223.577
2015-07-0118347.425
2015-10-0118378.803
2016-01-0118470.156
2016-04-0118656.207
\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", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
CPIAUCSLCPILFESL
DATE
2016-02-01237.336245.510
2016-03-01238.080245.913
2016-04-01238.992246.551
2016-05-01239.557247.137
2016-06-01240.222247.540
\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 }