{ "cells": [ { "cell_type": "markdown", "id": "c5def04f", "metadata": {}, "source": [ "" ] }, { "cell_type": "markdown", "id": "97d27415", "metadata": {}, "source": [ "### 1. 패키지(Package)\n", "- 설치하기 : pip install 패키지명\n", "- 로딩하기 : import 패키지명 as 애칭(별명)" ] }, { "cell_type": "code", "execution_count": 26, "id": "2a98cc43", "metadata": {}, "outputs": [], "source": [ "# 패키지 로딩하기\n", "import seaborn as sns # 데이터 시각화(Data Visualization)\n", "import pandas as pd # 데이터 읽어오기, 핸들링, 분석 " ] }, { "cell_type": "code", "execution_count": 23, "id": "428e4d2b", "metadata": {}, "outputs": [], "source": [ "# 예제 데이터 : diamonds\n", "diamonds = sns.load_dataset(\"diamonds\")" ] }, { "cell_type": "code", "execution_count": 24, "id": "27273539", "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", "
caratcutcolorclaritydepthtablepricexyz
00.23IdealESI261.555.03263.953.982.43
10.21PremiumESI159.861.03263.893.842.31
20.23GoodEVS156.965.03274.054.072.31
30.29PremiumIVS262.458.03344.204.232.63
40.31GoodJSI263.358.03354.344.352.75
\n", "
" ], "text/plain": [ " carat cut color clarity depth table price x y z\n", "0 0.23 Ideal E SI2 61.5 55.0 326 3.95 3.98 2.43\n", "1 0.21 Premium E SI1 59.8 61.0 326 3.89 3.84 2.31\n", "2 0.23 Good E VS1 56.9 65.0 327 4.05 4.07 2.31\n", "3 0.29 Premium I VS2 62.4 58.0 334 4.20 4.23 2.63\n", "4 0.31 Good J SI2 63.3 58.0 335 4.34 4.35 2.75" ] }, "execution_count": 24, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# 데이터의 일부 보기\n", "# data.head(n = 5)\n", "diamonds.head()" ] }, { "cell_type": "markdown", "id": "e584b421", "metadata": {}, "source": [ "### 2. 연산자(Operator)\n", "- 산술 연산자\n", "- 할당 연산자\n", "- 비교 연산자\n", "- 논리 연산자" ] }, { "cell_type": "code", "execution_count": 2, "id": "b6519086", "metadata": {}, "outputs": [], "source": [ "# (1) 산술 연산자\n", "# +, -, *, /, %, //, **\n", "# 기존에 없었던 새로운 열을 만들 때에 사용" ] }, { "cell_type": "code", "execution_count": 3, "id": "848eb0ee", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "7" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "3 + 4" ] }, { "cell_type": "code", "execution_count": 4, "id": "ed6c8c47", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "-1" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "3 - 4" ] }, { "cell_type": "code", "execution_count": 6, "id": "1ea5d465", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "12" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "3 * 4" ] }, { "cell_type": "code", "execution_count": 7, "id": "ee9311f7", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "0.75" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "3 / 4" ] }, { "cell_type": "code", "execution_count": 9, "id": "36938f4b", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "1" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "13 % 4 # 나머지" ] }, { "cell_type": "code", "execution_count": 11, "id": "affebe7c", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "3" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "13 // 4 # 몫" ] }, { "cell_type": "code", "execution_count": 13, "id": "62a08d19", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "28561" ] }, "execution_count": 13, "metadata": {}, "output_type": "execute_result" } ], "source": [ "13 ** 4 # 거듭제곱" ] }, { "cell_type": "code", "execution_count": 25, "id": "5d73f886", "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", "
caratcutcolorclaritydepthtablepricexyzxyz_mean
00.23IdealESI261.555.03263.953.982.433.453333
10.21PremiumESI159.861.03263.893.842.313.346667
20.23GoodEVS156.965.03274.054.072.313.476667
30.29PremiumIVS262.458.03344.204.232.633.686667
40.31GoodJSI263.358.03354.344.352.753.813333
\n", "
" ], "text/plain": [ " carat cut color clarity depth table price x y z \\\n", "0 0.23 Ideal E SI2 61.5 55.0 326 3.95 3.98 2.43 \n", "1 0.21 Premium E SI1 59.8 61.0 326 3.89 3.84 2.31 \n", "2 0.23 Good E VS1 56.9 65.0 327 4.05 4.07 2.31 \n", "3 0.29 Premium I VS2 62.4 58.0 334 4.20 4.23 2.63 \n", "4 0.31 Good J SI2 63.3 58.0 335 4.34 4.35 2.75 \n", "\n", " xyz_mean \n", "0 3.453333 \n", "1 3.346667 \n", "2 3.476667 \n", "3 3.686667 \n", "4 3.813333 " ] }, "execution_count": 25, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# xyz_mean : 새로운 열의 이름\n", "# (x + y + z)/3 \n", "# 평균 : 데이터를 다 더 하고, 자유로도 나눈 값\n", "\n", "diamonds[\"xyz_mean\"] = (diamonds[\"x\"] + diamonds[\"y\"] + diamonds[\"z\"])/3\n", "diamonds.head()" ] }, { "cell_type": "code", "execution_count": 14, "id": "7a5d0f43", "metadata": {}, "outputs": [], "source": [ "# (2) 할당 연산자\n", "# = \n", "# 저장하는 기능" ] }, { "cell_type": "code", "execution_count": 15, "id": "33c7faa7", "metadata": {}, "outputs": [], "source": [ "age = 29" ] }, { "cell_type": "code", "execution_count": 16, "id": "57c34868", "metadata": {}, "outputs": [], "source": [ "# age : data, RAM에 저장됨" ] }, { "cell_type": "code", "execution_count": 17, "id": "50180350", "metadata": {}, "outputs": [], "source": [ "name = \"허민\"" ] }, { "cell_type": "code", "execution_count": 18, "id": "c2123757", "metadata": {}, "outputs": [], "source": [ "# name : data, RAM에 저장됨" ] }, { "cell_type": "code", "execution_count": 19, "id": "d2e8ea2e", "metadata": {}, "outputs": [], "source": [ "# (3) 비교 연산자\n", "# >, >=, <, <=, ==, !=\n", "# 전체 데이터 중에서 조건에 맞는 데이터를 추출\n", "# 행 기준" ] }, { "cell_type": "code", "execution_count": 27, "id": "c099fe16", "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", " \n", " \n", "
caratcutcolorclaritydepthtablepricexyzxyz_mean
259984.01PremiumII161.061.01522310.1410.106.178.803333
259994.01PremiumJI162.562.01522310.029.946.248.733333
264444.00Very GoodII163.358.01598410.019.946.318.753333
271304.13FairHI164.861.01732910.009.856.438.760000
274155.01FairJI165.559.01801810.7410.546.989.420000
276304.50FairJI165.858.01853110.2310.166.729.036667
\n", "
" ], "text/plain": [ " carat cut color clarity depth table price x y \\\n", "25998 4.01 Premium I I1 61.0 61.0 15223 10.14 10.10 \n", "25999 4.01 Premium J I1 62.5 62.0 15223 10.02 9.94 \n", "26444 4.00 Very Good I I1 63.3 58.0 15984 10.01 9.94 \n", "27130 4.13 Fair H I1 64.8 61.0 17329 10.00 9.85 \n", "27415 5.01 Fair J I1 65.5 59.0 18018 10.74 10.54 \n", "27630 4.50 Fair J I1 65.8 58.0 18531 10.23 10.16 \n", "\n", " z xyz_mean \n", "25998 6.17 8.803333 \n", "25999 6.24 8.733333 \n", "26444 6.31 8.753333 \n", "27130 6.43 8.760000 \n", "27415 6.98 9.420000 \n", "27630 6.72 9.036667 " ] }, "execution_count": 27, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# i. carat(무게)가 4 이상인 다이아몬드\n", "# data.loc[행, :]\n", "\n", "diamonds.loc[diamonds[\"carat\"] >= 4 , :]" ] }, { "cell_type": "code", "execution_count": 28, "id": "4bc27c14", "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", " \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", "
caratcutcolorclaritydepthtablepricexyzxyz_mean
20.23GoodEVS156.965.03274.054.072.313.476667
40.31GoodJSI263.358.03354.344.352.753.813333
100.30GoodJSI164.055.03394.254.282.733.753333
170.30GoodJSI163.454.03514.234.292.703.740000
180.30GoodJSI163.856.03514.234.262.713.733333
....................................
539130.80GoodGVS264.258.027535.845.813.745.130000
539140.84GoodIVS163.759.027535.945.903.775.203333
539160.74GoodDSI163.159.027535.715.743.615.020000
539270.79GoodFSI158.159.027566.066.133.545.243333
539360.72GoodDSI163.155.027575.695.753.615.016667
\n", "

4906 rows × 11 columns

\n", "
" ], "text/plain": [ " carat cut color clarity depth table price x y z \\\n", "2 0.23 Good E VS1 56.9 65.0 327 4.05 4.07 2.31 \n", "4 0.31 Good J SI2 63.3 58.0 335 4.34 4.35 2.75 \n", "10 0.30 Good J SI1 64.0 55.0 339 4.25 4.28 2.73 \n", "17 0.30 Good J SI1 63.4 54.0 351 4.23 4.29 2.70 \n", "18 0.30 Good J SI1 63.8 56.0 351 4.23 4.26 2.71 \n", "... ... ... ... ... ... ... ... ... ... ... \n", "53913 0.80 Good G VS2 64.2 58.0 2753 5.84 5.81 3.74 \n", "53914 0.84 Good I VS1 63.7 59.0 2753 5.94 5.90 3.77 \n", "53916 0.74 Good D SI1 63.1 59.0 2753 5.71 5.74 3.61 \n", "53927 0.79 Good F SI1 58.1 59.0 2756 6.06 6.13 3.54 \n", "53936 0.72 Good D SI1 63.1 55.0 2757 5.69 5.75 3.61 \n", "\n", " xyz_mean \n", "2 3.476667 \n", "4 3.813333 \n", "10 3.753333 \n", "17 3.740000 \n", "18 3.733333 \n", "... ... \n", "53913 5.130000 \n", "53914 5.203333 \n", "53916 5.020000 \n", "53927 5.243333 \n", "53936 5.016667 \n", "\n", "[4906 rows x 11 columns]" ] }, "execution_count": 28, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# ii. cut(품질)이 \"Good\"인 다이아몬드\n", "# data.loc[행, :]\n", "\n", "diamonds.loc[diamonds[\"cut\"] == \"Good\" , :]" ] }, { "cell_type": "code", "execution_count": 30, "id": "4e3c4e8e", "metadata": {}, "outputs": [], "source": [ "# (4) 논리 연산자\n", "# &, |\n", "# 조건을 2개 이상 줄 때\n", "\n", "# & : and : 2개 이상의 조건을 모두 만족하는 데이터\n", "# | : or : 2개 이상의 조건 중에서 하나만 만족해도 데이터" ] }, { "cell_type": "code", "execution_count": 32, "id": "02abd635", "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", " \n", " \n", "
caratcutcolorclaritydepthtablepricexyzxyz_mean
228313.00GoodII157.064.0108639.389.315.338.006667
235393.00GoodEI164.265.0115489.088.965.797.943333
258503.00GoodJSI259.364.0149189.329.195.508.003333
275173.01GoodISI263.960.0182429.069.015.777.946667
275183.01GoodISI263.960.0182429.069.015.777.946667
276493.01GoodHSI257.664.0185939.449.385.428.080000
\n", "
" ], "text/plain": [ " carat cut color clarity depth table price x y z \\\n", "22831 3.00 Good I I1 57.0 64.0 10863 9.38 9.31 5.33 \n", "23539 3.00 Good E I1 64.2 65.0 11548 9.08 8.96 5.79 \n", "25850 3.00 Good J SI2 59.3 64.0 14918 9.32 9.19 5.50 \n", "27517 3.01 Good I SI2 63.9 60.0 18242 9.06 9.01 5.77 \n", "27518 3.01 Good I SI2 63.9 60.0 18242 9.06 9.01 5.77 \n", "27649 3.01 Good H SI2 57.6 64.0 18593 9.44 9.38 5.42 \n", "\n", " xyz_mean \n", "22831 8.006667 \n", "23539 7.943333 \n", "25850 8.003333 \n", "27517 7.946667 \n", "27518 7.946667 \n", "27649 8.080000 " ] }, "execution_count": 32, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# i. carat은 3이상이고, cut은 \"Good\"인 데이터\n", "diamonds.loc[(diamonds[\"carat\"] >= 3) & (diamonds[\"cut\"] == \"Good\") , :]" ] }, { "cell_type": "code", "execution_count": 33, "id": "227ea216", "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", " \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", "
caratcutcolorclaritydepthtablepricexyzxyz_mean
20.23GoodEVS156.965.03274.054.072.313.476667
40.31GoodJSI263.358.03354.344.352.753.813333
100.30GoodJSI164.055.03394.254.282.733.753333
170.30GoodJSI163.454.03514.234.292.703.740000
180.30GoodJSI163.856.03514.234.262.713.733333
....................................
539130.80GoodGVS264.258.027535.845.813.745.130000
539140.84GoodIVS163.759.027535.945.903.775.203333
539160.74GoodDSI163.159.027535.715.743.615.020000
539270.79GoodFSI158.159.027566.066.133.545.243333
539360.72GoodDSI163.155.027575.695.753.615.016667
\n", "

4940 rows × 11 columns

\n", "
" ], "text/plain": [ " carat cut color clarity depth table price x y z \\\n", "2 0.23 Good E VS1 56.9 65.0 327 4.05 4.07 2.31 \n", "4 0.31 Good J SI2 63.3 58.0 335 4.34 4.35 2.75 \n", "10 0.30 Good J SI1 64.0 55.0 339 4.25 4.28 2.73 \n", "17 0.30 Good J SI1 63.4 54.0 351 4.23 4.29 2.70 \n", "18 0.30 Good J SI1 63.8 56.0 351 4.23 4.26 2.71 \n", "... ... ... ... ... ... ... ... ... ... ... \n", "53913 0.80 Good G VS2 64.2 58.0 2753 5.84 5.81 3.74 \n", "53914 0.84 Good I VS1 63.7 59.0 2753 5.94 5.90 3.77 \n", "53916 0.74 Good D SI1 63.1 59.0 2753 5.71 5.74 3.61 \n", "53927 0.79 Good F SI1 58.1 59.0 2756 6.06 6.13 3.54 \n", "53936 0.72 Good D SI1 63.1 55.0 2757 5.69 5.75 3.61 \n", "\n", " xyz_mean \n", "2 3.476667 \n", "4 3.813333 \n", "10 3.753333 \n", "17 3.740000 \n", "18 3.733333 \n", "... ... \n", "53913 5.130000 \n", "53914 5.203333 \n", "53916 5.020000 \n", "53927 5.243333 \n", "53936 5.016667 \n", "\n", "[4940 rows x 11 columns]" ] }, "execution_count": 33, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# ii. carat은 3이상이거나 cut은 \"Good\"인 데이터\n", "diamonds.loc[(diamonds[\"carat\"] >= 3) | (diamonds[\"cut\"] == \"Good\") , :]" ] }, { "cell_type": "markdown", "id": "2a06be26", "metadata": {}, "source": [ "### 3. 데이터의 유형(Type of Data)\n", "- 수치형 : Numerical : 정수형(integer, int)\n", "- 수치형 : Numerical : 실수형(float)\n", "- 문자형 : String, Object\n", "- bool형 : Boolean : True, False" ] }, { "cell_type": "code", "execution_count": 34, "id": "a6c835b6", "metadata": {}, "outputs": [], "source": [ "# (1) 수치형(정수형)\n", "x1 = 10" ] }, { "cell_type": "code", "execution_count": 35, "id": "c3f1c4b3", "metadata": {}, "outputs": [], "source": [ "# x1 : data\n", "# type : int" ] }, { "cell_type": "code", "execution_count": 36, "id": "8ec6f923", "metadata": {}, "outputs": [], "source": [ "x2 = 10.2" ] }, { "cell_type": "code", "execution_count": 37, "id": "2674a877", "metadata": {}, "outputs": [], "source": [ "# x2 : data\n", "# type : float" ] }, { "cell_type": "code", "execution_count": 39, "id": "094fedfd", "metadata": {}, "outputs": [], "source": [ "x3 = '인생은 고통의 바다이다.'" ] }, { "cell_type": "code", "execution_count": 40, "id": "710d289c", "metadata": {}, "outputs": [], "source": [ "# x3 : data\n", "# type : object" ] }, { "cell_type": "code", "execution_count": 41, "id": "f2b55c5e", "metadata": {}, "outputs": [], "source": [ "x4 = \"문제가 문제가 아니라 문제를 대하는 나의 태도가 문제다.\"" ] }, { "cell_type": "code", "execution_count": 42, "id": "3feefd1e", "metadata": {}, "outputs": [], "source": [ "# x4 : data\n", "# type : object" ] }, { "cell_type": "code", "execution_count": 43, "id": "8beedebe", "metadata": {}, "outputs": [], "source": [ "x5 = True" ] }, { "cell_type": "code", "execution_count": 44, "id": "3e738c86", "metadata": {}, "outputs": [], "source": [ "# x5 : data\n", "# type : bool" ] }, { "cell_type": "code", "execution_count": 45, "id": "b1f4492b", "metadata": {}, "outputs": [], "source": [ "x6 = False" ] }, { "cell_type": "code", "execution_count": 46, "id": "51f6e31a", "metadata": {}, "outputs": [], "source": [ "# x6 : data\n", "# type : bool" ] }, { "cell_type": "code", "execution_count": 47, "id": "189d166c", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n", "RangeIndex: 53940 entries, 0 to 53939\n", "Data columns (total 11 columns):\n", " # Column Non-Null Count Dtype \n", "--- ------ -------------- ----- \n", " 0 carat 53940 non-null float64 \n", " 1 cut 53940 non-null category\n", " 2 color 53940 non-null category\n", " 3 clarity 53940 non-null category\n", " 4 depth 53940 non-null float64 \n", " 5 table 53940 non-null float64 \n", " 6 price 53940 non-null int64 \n", " 7 x 53940 non-null float64 \n", " 8 y 53940 non-null float64 \n", " 9 z 53940 non-null float64 \n", " 10 xyz_mean 53940 non-null float64 \n", "dtypes: category(3), float64(7), int64(1)\n", "memory usage: 3.4 MB\n" ] } ], "source": [ "# 데이터의 정보(Information) 보기\n", "# data.info()\n", "diamonds.info()" ] }, { "cell_type": "markdown", "id": "93736c7f", "metadata": {}, "source": [ "### 4. 데이터(Data)\n", "- 리스트(list) *****\n", "- 튜플(tuple)\n", "- 딕셔너리(dictionary)\n", "- 시리즈(series)\n", "- 데이터프레임(DataFrame) *****" ] }, { "cell_type": "code", "execution_count": 48, "id": "b550f5bd", "metadata": {}, "outputs": [], "source": [ "# (1) 리스트(List)\n", "# 가장 많이 사용하는 데이터 형태 중에 하나\n", "# 활용도\n", "# - 함수의 파라미터 설정에 2개 이상을 저장할 때\n", "# - 슬라이싱(Slicing)\n", "\n", "# 리스트 안에 있는 값은 수정이 가능함" ] }, { "cell_type": "code", "execution_count": 49, "id": "cd96b2e7", "metadata": {}, "outputs": [], "source": [ "# i. 리스트 만들기\n", "# [값, 값, ...] : 1차원 구조\n", "# [[값, 값, ...], [값, 값, ...]] : 2차원 구조" ] }, { "cell_type": "code", "execution_count": 50, "id": "4c4d01da", "metadata": {}, "outputs": [], "source": [ "major = [\"통계학\", \"컴퓨터공학\", \"컴퓨터과학\", \"컴퓨터공학\", \"경제학\", \"수학\", \"컴퓨터공학\", \"수학\"]" ] }, { "cell_type": "code", "execution_count": 51, "id": "6736c42f", "metadata": {}, "outputs": [], "source": [ "# major : data : list\n", "# type : object" ] }, { "cell_type": "code", "execution_count": 52, "id": "f2fb8b45", "metadata": {}, "outputs": [], "source": [ "sleep = [6, 8, 7, 6, 6]" ] }, { "cell_type": "code", "execution_count": 53, "id": "db0d0b68", "metadata": {}, "outputs": [], "source": [ "# sleep : data : list\n", "# type : int" ] }, { "cell_type": "code", "execution_count": 54, "id": "4dae2e48", "metadata": {}, "outputs": [], "source": [ "smoke = [False, False, True, False, False]" ] }, { "cell_type": "code", "execution_count": 55, "id": "b664826e", "metadata": {}, "outputs": [], "source": [ "# smoke : data : list\n", "# type : bool" ] }, { "cell_type": "code", "execution_count": 59, "id": "d09b77e6", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "10" ] }, "execution_count": 59, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# ii. 리스트 슬라이싱\n", "# list[index]\n", "# index : 위치, 행(row), 행의 이름(row name)\n", "# 파이썬에서는 첫 번째 위치를 0\n", "sleep[0]" ] }, { "cell_type": "code", "execution_count": 60, "id": "4a9a2ad3", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[10, 8]" ] }, "execution_count": 60, "metadata": {}, "output_type": "execute_result" } ], "source": [ "sleep[:2] # 파이썬 관점 : 0, 1번째를 슬라이싱" ] }, { "cell_type": "code", "execution_count": 61, "id": "754a4c22", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[7, 6, 6]" ] }, "execution_count": 61, "metadata": {}, "output_type": "execute_result" } ], "source": [ "sleep[2:] # 파이썬 관점 : 2, 3, ... 끝까지 슬라이싱" ] }, { "cell_type": "code", "execution_count": 62, "id": "2b7b934a", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[10, 7, 6]" ] }, "execution_count": 62, "metadata": {}, "output_type": "execute_result" } ], "source": [ "sleep[::2] # 한 칸씩 건너 뛰면서 슬라이싱" ] }, { "cell_type": "code", "execution_count": 63, "id": "1ba8575b", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "6" ] }, "execution_count": 63, "metadata": {}, "output_type": "execute_result" } ], "source": [ "sleep[-1] # 제일 오른 쪽에 있는 값" ] }, { "cell_type": "code", "execution_count": 65, "id": "aac44a81", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[10, 8, 7, 6, 6]" ] }, "execution_count": 65, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# iii. 리스트의 값 수정하기\n", "sleep[0] = 10\n", "sleep" ] }, { "cell_type": "code", "execution_count": null, "id": "ab23c3a7", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": 66, "id": "72dc5a86", "metadata": {}, "outputs": [], "source": [ "# (2) 튜플(Tuple)\n", "# 리스트와 거의 비슷함\n", "# 값이 수정 불가\n", "# 초기값을 설정, 어떤 값이 변경되면 안될 때" ] }, { "cell_type": "code", "execution_count": 67, "id": "42e19b5b", "metadata": {}, "outputs": [], "source": [ "# i. 튜플 만들기\n", "# (값, 값, ....)" ] }, { "cell_type": "code", "execution_count": 68, "id": "b5157de2", "metadata": {}, "outputs": [], "source": [ "address = (\"분당\", \"부천\", \"서울\", \"부천\")" ] }, { "cell_type": "code", "execution_count": 69, "id": "9720ef05", "metadata": {}, "outputs": [], "source": [ "# address : data : tuple\n", "# type : object" ] }, { "cell_type": "code", "execution_count": 70, "id": "dc5b34bf", "metadata": {}, "outputs": [], "source": [ "cyear = (17, 5, 1)" ] }, { "cell_type": "code", "execution_count": 71, "id": "8c2ef4a3", "metadata": {}, "outputs": [], "source": [ "# cyear : data : tuple\n", "# type : int" ] }, { "cell_type": "code", "execution_count": null, "id": "02800693", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": 72, "id": "28d08eb1", "metadata": {}, "outputs": [], "source": [ "# (3) 딕셔너리(Dictionary)\n", "# 키(key)와 값(value)로 이루어진 데이터 형태" ] }, { "cell_type": "code", "execution_count": 73, "id": "9a411062", "metadata": {}, "outputs": [], "source": [ "# i. 딕셔너리 만들기\n", "# {\"key1\" : [value1, value2, ...],\n", "# \"key2\" : [value1, value2, ...]}" ] }, { "cell_type": "code", "execution_count": 74, "id": "3e75dc5c", "metadata": {}, "outputs": [], "source": [ "survey = {\"id\" : [1, 2, 3, 4],\n", " \"nation\" : [\"한국\", \"스위스\", \"한국\", \"미국\"],\n", " \"car\" : [\"제네시스\", \"아반떼\", \"아이오닉5\", \"A4\"]}" ] }, { "cell_type": "code", "execution_count": 75, "id": "a24ad163", "metadata": {}, "outputs": [], "source": [ "# survey : data : dictionary\n", "# key : id, nation, car\n", "# value(id) : 1, 2, 3, 4" ] }, { "cell_type": "code", "execution_count": 80, "id": "14bb4879", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "dict_items([('id', [1, 2, 3, 4]), ('nation', ['한국', '스위스', '한국', '미국']), ('car', ['제네시스', '아반떼', '아이오닉5', 'A4'])])" ] }, "execution_count": 80, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# items : key + value\n", "survey.items()" ] }, { "cell_type": "code", "execution_count": null, "id": "4e22b4a9", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": 81, "id": "167da52e", "metadata": {}, "outputs": [], "source": [ "# (4) 시리즈(Series)\n", "# 행과 열로 데이터 있는 데이터에서\n", "# 하나의 행 또는 하나의 열을 슬라이싱하면" ] }, { "cell_type": "code", "execution_count": null, "id": "f87cb2c6", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": 82, "id": "b40d9fae", "metadata": {}, "outputs": [], "source": [ "# (5) 데이터프레임(DataFrame)\n", "# 행과 열로 구성되어 있음 : 2차원 구조\n", "# 열마다 같은 데이터 유형을 가져되고, 안 가져되 됨\n", "\n", "# 참고로\n", "# 모든 열이 하나의 데이터 유형만 가지는 행과 열로 된 것 : 행렬" ] }, { "cell_type": "code", "execution_count": 83, "id": "ea28a219", "metadata": {}, "outputs": [], "source": [ "# i. 데이터 프레임 만들기\n", "# pandas.DataFrame(딕셔너리)" ] }, { "cell_type": "code", "execution_count": 84, "id": "1a845a2f", "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", "
idnationcar
01한국제네시스
12스위스아반떼
23한국아이오닉5
34미국A4
\n", "
" ], "text/plain": [ " id nation car\n", "0 1 한국 제네시스\n", "1 2 스위스 아반떼\n", "2 3 한국 아이오닉5\n", "3 4 미국 A4" ] }, "execution_count": 84, "metadata": {}, "output_type": "execute_result" } ], "source": [ "df = pd.DataFrame(survey)\n", "df # 데이터 보기 : 데이터의 양이 적을 때에 사용함" ] }, { "cell_type": "markdown", "id": "c94645f1", "metadata": {}, "source": [ "### 5. 데이터 읽어오기\n", "- txt\n", "- csv : comma separated value\n", "- excel\n", "- pickle\n", "- etc" ] }, { "cell_type": "code", "execution_count": 86, "id": "8a3f41f3", "metadata": {}, "outputs": [], "source": [ "# (1) txt\n", "# pandas.read_table(filepath_or_buffer = \"드라이브:/폴더/파일명.txt\",\n", "# sep = 구분자,\n", "# header = ,\n", "# encoding = \"EUC-KR\")" ] }, { "cell_type": "code", "execution_count": 89, "id": "06c99e7d", "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", "
idfooddrink
01피자소주
12돼지갈비소주
23삼겹살콜라
34닭갈비소주
\n", "
" ], "text/plain": [ " id food drink\n", "0 1 피자 소주\n", "1 2 돼지갈비 소주\n", "2 3 삼겹살 콜라\n", "3 4 닭갈비 소주" ] }, "execution_count": 89, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# i. 구분자(Separater) : 공백(blank)\n", "blank = pd.read_table(filepath_or_buffer = \"d:/DB/blank.txt\",\n", " sep = \" \",\n", " header = 0)\n", "blank # DataFrame : 행과 열로 되어 있음" ] }, { "cell_type": "code", "execution_count": 90, "id": "332256ef", "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", "
idhobbymoney
01야구50
12축구100
23애니메이션70
34영화보기80
45그림그리기70
56영화보기80
67책사기300
\n", "
" ], "text/plain": [ " id hobby money\n", "0 1 야구 50\n", "1 2 축구 100\n", "2 3 애니메이션 70\n", "3 4 영화보기 80\n", "4 5 그림그리기 70\n", "5 6 영화보기 80\n", "6 7 책사기 300" ] }, "execution_count": 90, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# ii. 구분자(Separater) : 콤마(comma)\n", "comma = pd.read_table(filepath_or_buffer = \"d:/DB/comma.txt\",\n", " sep = \",\",\n", " header = 0)\n", "comma" ] }, { "cell_type": "code", "execution_count": 91, "id": "e7fa1fb0", "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", "
idweightheight
0170175
1248158
2360170
3470180
45110195
\n", "
" ], "text/plain": [ " id weight height\n", "0 1 70 175\n", "1 2 48 158\n", "2 3 60 170\n", "3 4 70 180\n", "4 5 110 195" ] }, "execution_count": 91, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# iii. 구분자(Separater) : 탭(tab)\n", "tab = pd.read_table(filepath_or_buffer = \"d:/DB/tab.txt\",\n", " sep = \"\\t\",\n", " header = 0)\n", "tab" ] }, { "cell_type": "code", "execution_count": 92, "id": "452ab9cd", "metadata": {}, "outputs": [], "source": [ "# (2) csv : comma separated value\n", "# 엑셀의 특수한 형태\n", "# pandas.read_csv(filepath_or_buffer = \"드라이브/폴더/파일명.csv\",\n", "# header = ,\n", "# encoding = \"EUC-KR\")" ] }, { "cell_type": "code", "execution_count": 94, "id": "638da291", "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", "
ididolmyear
01차은우30
12박보검29
23고윤정31
34지효31
\n", "
" ], "text/plain": [ " id idol myear\n", "0 1 차은우 30\n", "1 2 박보검 29\n", "2 3 고윤정 31\n", "3 4 지효 31" ] }, "execution_count": 94, "metadata": {}, "output_type": "execute_result" } ], "source": [ "hope = pd.read_csv(filepath_or_buffer = \"d:/DB/hope.csv\",\n", " header = 0,\n", " encoding = \"EUC-KR\")\n", "hope" ] }, { "cell_type": "code", "execution_count": 95, "id": "b9522168", "metadata": {}, "outputs": [], "source": [ "# (3) 엑셀 : xls, xlsx\n", "# pandas.read_excle(io = \"드라이브/폴더/파일명.xlsx\",\n", "# sheet_name = index or \"name\",\n", "# header = )" ] }, { "cell_type": "code", "execution_count": 96, "id": "62d3436e", "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", "
idbooksauthor
012NaN
123김영하
231박민규
\n", "
" ], "text/plain": [ " id books author\n", "0 1 2 NaN\n", "1 2 3 김영하\n", "2 3 1 박민규" ] }, "execution_count": 96, "metadata": {}, "output_type": "execute_result" } ], "source": [ "reading = pd.read_excel(io = \"d:/DB/reading.xlsx\",\n", " sheet_name = 0,\n", " header = 0)\n", "reading" ] }, { "cell_type": "code", "execution_count": 97, "id": "edd11f1f", "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", "
idbooksauthor
012NaN
123김영하
231박민규
\n", "
" ], "text/plain": [ " id books author\n", "0 1 2 NaN\n", "1 2 3 김영하\n", "2 3 1 박민규" ] }, "execution_count": 97, "metadata": {}, "output_type": "execute_result" } ], "source": [ "reading = pd.read_excel(io = \"d:/DB/reading.xlsx\",\n", " sheet_name = \"data\",\n", " header = 0)\n", "reading" ] }, { "cell_type": "code", "execution_count": 105, "id": "30656a5c", "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", "
idbooksauthor
012NaN
123김영하
231박민규
\n", "
" ], "text/plain": [ " id books author\n", "0 1 2 NaN\n", "1 2 3 김영하\n", "2 3 1 박민규" ] }, "execution_count": 105, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# (4) pickle\n", "# pandas.read_pickle(filepath_of_buffer = \"드라이브/폴더/파일명.pickle\")\n", "reading2 = pd.read_pickle(filepath_or_buffer = \"d:/DB/reading_2024_0308_1716.pickle\")\n", "reading2" ] }, { "cell_type": "markdown", "id": "2ad7e98f", "metadata": {}, "source": [ "### 6. 데이터 저장하기 \n", "- RAM -> HDD\n", "- txt\n", "- csv\n", "- excel\n", "- pickle" ] }, { "cell_type": "code", "execution_count": 100, "id": "f2df1c6d", "metadata": {}, "outputs": [], "source": [ "# (1) txt\n", "# data.to_csv(path_or_buf = \"드라이브/폴더/파일명.txt\",\n", "# sep = \" \" or \",\" or \"\\t\")\n", "\n", "reading.to_csv(path_or_buf = \"d:/DB/reading_2024_0308_1706.txt\",\n", " sep = \" \",\n", " index = False)\n", "\n", "# 참고\n", "# index : 행의 이름\n", "# index = False : 행의 이름은 저장하지 마세요." ] }, { "cell_type": "code", "execution_count": 102, "id": "9c8cf8d5", "metadata": {}, "outputs": [], "source": [ "# (2) csv\n", "# data.to_csv(path_or_buf = \"드라이브/폴더/파일명.csv\")\n", "\n", "reading.to_csv(path_or_buf = \"d:/DB/reading_2024_0308_1710.csv\",\n", " index = False,\n", " encoding = \"EUC-KR\")" ] }, { "cell_type": "code", "execution_count": 103, "id": "268c7e59", "metadata": {}, "outputs": [], "source": [ "# (3) excel\n", "# data.to_excel(excel_writer = \"드라이브/폴더/파일명.xlsx\",\n", "# sheet_name = \"name\",\n", "# index = False)\n", "\n", "reading.to_excel(excel_writer = \"d:/DB/reading_2024_0308_1713.xlsx\",\n", " sheet_name = \"data2\",\n", " index = False)" ] }, { "cell_type": "code", "execution_count": 104, "id": "a96d08af", "metadata": {}, "outputs": [], "source": [ "# (4) pickle\n", "# data.to_pickle(path = \"드라이브/폴더/파일명.pickle\")\n", "reading.to_pickle(path = \"d:/DB/reading_2024_0308_1716.pickle\")" ] }, { "cell_type": "code", "execution_count": null, "id": "93f2a66c", "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "hide_input": false, "kernelspec": { "display_name": "Python 3 (ipykernel)", "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.11.5" }, "varInspector": { "cols": { "lenName": 16, "lenType": 16, "lenVar": 40 }, "kernels_config": { "python": { "delete_cmd_postfix": "", "delete_cmd_prefix": "del ", "library": "var_list.py", "varRefreshCmd": "print(var_dic_list())" }, "r": { "delete_cmd_postfix": ") ", "delete_cmd_prefix": "rm(", "library": "var_list.r", "varRefreshCmd": "cat(var_dic_list()) " } }, "types_to_exclude": [ "module", "function", "builtin_function_or_method", "instance", "_Feature" ], "window_display": false } }, "nbformat": 4, "nbformat_minor": 5 }