Created
September 9, 2021 02:51
-
-
Save sksmsWKd/b66c36a0db5efc1c506948d803226c5b to your computer and use it in GitHub Desktop.
Pandas tutorial 01
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"cells": [ | |
{ | |
"cell_type": "code", | |
"execution_count": 2, | |
"id": "800619a8", | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"import pandas as pd # 1. pandas import" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 4, | |
"id": "07dc2a8b", | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"df = pd.read_csv(\"fortune500.csv\")" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 5, | |
"id": "243a698d", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/html": [ | |
"<div>\n", | |
"<style scoped>\n", | |
" .dataframe tbody tr th:only-of-type {\n", | |
" vertical-align: middle;\n", | |
" }\n", | |
"\n", | |
" .dataframe tbody tr th {\n", | |
" vertical-align: top;\n", | |
" }\n", | |
"\n", | |
" .dataframe thead th {\n", | |
" text-align: right;\n", | |
" }\n", | |
"</style>\n", | |
"<table border=\"1\" class=\"dataframe\">\n", | |
" <thead>\n", | |
" <tr style=\"text-align: right;\">\n", | |
" <th></th>\n", | |
" <th>Year</th>\n", | |
" <th>Rank</th>\n", | |
" <th>Company</th>\n", | |
" <th>Revenue (in millions)</th>\n", | |
" <th>Profit (in millions)</th>\n", | |
" </tr>\n", | |
" </thead>\n", | |
" <tbody>\n", | |
" <tr>\n", | |
" <th>0</th>\n", | |
" <td>1955</td>\n", | |
" <td>1</td>\n", | |
" <td>General Motors</td>\n", | |
" <td>9823.5</td>\n", | |
" <td>806</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th>1</th>\n", | |
" <td>1955</td>\n", | |
" <td>2</td>\n", | |
" <td>Exxon Mobil</td>\n", | |
" <td>5661.4</td>\n", | |
" <td>584.8</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th>2</th>\n", | |
" <td>1955</td>\n", | |
" <td>3</td>\n", | |
" <td>U.S. Steel</td>\n", | |
" <td>3250.4</td>\n", | |
" <td>195.4</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th>3</th>\n", | |
" <td>1955</td>\n", | |
" <td>4</td>\n", | |
" <td>General Electric</td>\n", | |
" <td>2959.1</td>\n", | |
" <td>212.6</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th>4</th>\n", | |
" <td>1955</td>\n", | |
" <td>5</td>\n", | |
" <td>Esmark</td>\n", | |
" <td>2510.8</td>\n", | |
" <td>19.1</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th>...</th>\n", | |
" <td>...</td>\n", | |
" <td>...</td>\n", | |
" <td>...</td>\n", | |
" <td>...</td>\n", | |
" <td>...</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th>25495</th>\n", | |
" <td>2005</td>\n", | |
" <td>496</td>\n", | |
" <td>Wm. Wrigley Jr.</td>\n", | |
" <td>3648.6</td>\n", | |
" <td>493</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th>25496</th>\n", | |
" <td>2005</td>\n", | |
" <td>497</td>\n", | |
" <td>Peabody Energy</td>\n", | |
" <td>3631.6</td>\n", | |
" <td>175.4</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th>25497</th>\n", | |
" <td>2005</td>\n", | |
" <td>498</td>\n", | |
" <td>Wendy's International</td>\n", | |
" <td>3630.4</td>\n", | |
" <td>57.8</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th>25498</th>\n", | |
" <td>2005</td>\n", | |
" <td>499</td>\n", | |
" <td>Kindred Healthcare</td>\n", | |
" <td>3616.6</td>\n", | |
" <td>70.6</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th>25499</th>\n", | |
" <td>2005</td>\n", | |
" <td>500</td>\n", | |
" <td>Cincinnati Financial</td>\n", | |
" <td>3614.0</td>\n", | |
" <td>584</td>\n", | |
" </tr>\n", | |
" </tbody>\n", | |
"</table>\n", | |
"<p>25500 rows × 5 columns</p>\n", | |
"</div>" | |
], | |
"text/plain": [ | |
" Year Rank Company Revenue (in millions) \\\n", | |
"0 1955 1 General Motors 9823.5 \n", | |
"1 1955 2 Exxon Mobil 5661.4 \n", | |
"2 1955 3 U.S. Steel 3250.4 \n", | |
"3 1955 4 General Electric 2959.1 \n", | |
"4 1955 5 Esmark 2510.8 \n", | |
"... ... ... ... ... \n", | |
"25495 2005 496 Wm. Wrigley Jr. 3648.6 \n", | |
"25496 2005 497 Peabody Energy 3631.6 \n", | |
"25497 2005 498 Wendy's International 3630.4 \n", | |
"25498 2005 499 Kindred Healthcare 3616.6 \n", | |
"25499 2005 500 Cincinnati Financial 3614.0 \n", | |
"\n", | |
" Profit (in millions) \n", | |
"0 806 \n", | |
"1 584.8 \n", | |
"2 195.4 \n", | |
"3 212.6 \n", | |
"4 19.1 \n", | |
"... ... \n", | |
"25495 493 \n", | |
"25496 175.4 \n", | |
"25497 57.8 \n", | |
"25498 70.6 \n", | |
"25499 584 \n", | |
"\n", | |
"[25500 rows x 5 columns]" | |
] | |
}, | |
"execution_count": 5, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"df #보고자 하면 그냥 변수명만 눌러도 되고" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 6, | |
"id": "582c6d19", | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"pd.set_option(\"display.max_columns\",3)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 7, | |
"id": "4c79e720", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/html": [ | |
"<div>\n", | |
"<style scoped>\n", | |
" .dataframe tbody tr th:only-of-type {\n", | |
" vertical-align: middle;\n", | |
" }\n", | |
"\n", | |
" .dataframe tbody tr th {\n", | |
" vertical-align: top;\n", | |
" }\n", | |
"\n", | |
" .dataframe thead th {\n", | |
" text-align: right;\n", | |
" }\n", | |
"</style>\n", | |
"<table border=\"1\" class=\"dataframe\">\n", | |
" <thead>\n", | |
" <tr style=\"text-align: right;\">\n", | |
" <th></th>\n", | |
" <th>Year</th>\n", | |
" <th>...</th>\n", | |
" <th>Profit (in millions)</th>\n", | |
" </tr>\n", | |
" </thead>\n", | |
" <tbody>\n", | |
" <tr>\n", | |
" <th>0</th>\n", | |
" <td>1955</td>\n", | |
" <td>...</td>\n", | |
" <td>806</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th>1</th>\n", | |
" <td>1955</td>\n", | |
" <td>...</td>\n", | |
" <td>584.8</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th>2</th>\n", | |
" <td>1955</td>\n", | |
" <td>...</td>\n", | |
" <td>195.4</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th>3</th>\n", | |
" <td>1955</td>\n", | |
" <td>...</td>\n", | |
" <td>212.6</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th>4</th>\n", | |
" <td>1955</td>\n", | |
" <td>...</td>\n", | |
" <td>19.1</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th>...</th>\n", | |
" <td>...</td>\n", | |
" <td>...</td>\n", | |
" <td>...</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th>25495</th>\n", | |
" <td>2005</td>\n", | |
" <td>...</td>\n", | |
" <td>493</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th>25496</th>\n", | |
" <td>2005</td>\n", | |
" <td>...</td>\n", | |
" <td>175.4</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th>25497</th>\n", | |
" <td>2005</td>\n", | |
" <td>...</td>\n", | |
" <td>57.8</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th>25498</th>\n", | |
" <td>2005</td>\n", | |
" <td>...</td>\n", | |
" <td>70.6</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th>25499</th>\n", | |
" <td>2005</td>\n", | |
" <td>...</td>\n", | |
" <td>584</td>\n", | |
" </tr>\n", | |
" </tbody>\n", | |
"</table>\n", | |
"<p>25500 rows × 5 columns</p>\n", | |
"</div>" | |
], | |
"text/plain": [ | |
" Year ... Profit (in millions)\n", | |
"0 1955 ... 806\n", | |
"1 1955 ... 584.8\n", | |
"2 1955 ... 195.4\n", | |
"3 1955 ... 212.6\n", | |
"4 1955 ... 19.1\n", | |
"... ... ... ...\n", | |
"25495 2005 ... 493\n", | |
"25496 2005 ... 175.4\n", | |
"25497 2005 ... 57.8\n", | |
"25498 2005 ... 70.6\n", | |
"25499 2005 ... 584\n", | |
"\n", | |
"[25500 rows x 5 columns]" | |
] | |
}, | |
"execution_count": 7, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"df" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 8, | |
"id": "539b91d8", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/html": [ | |
"<div>\n", | |
"<style scoped>\n", | |
" .dataframe tbody tr th:only-of-type {\n", | |
" vertical-align: middle;\n", | |
" }\n", | |
"\n", | |
" .dataframe tbody tr th {\n", | |
" vertical-align: top;\n", | |
" }\n", | |
"\n", | |
" .dataframe thead th {\n", | |
" text-align: right;\n", | |
" }\n", | |
"</style>\n", | |
"<table border=\"1\" class=\"dataframe\">\n", | |
" <thead>\n", | |
" <tr style=\"text-align: right;\">\n", | |
" <th></th>\n", | |
" <th>Year</th>\n", | |
" <th>...</th>\n", | |
" <th>Profit (in millions)</th>\n", | |
" </tr>\n", | |
" </thead>\n", | |
" <tbody>\n", | |
" <tr>\n", | |
" <th>0</th>\n", | |
" <td>1955</td>\n", | |
" <td>...</td>\n", | |
" <td>806</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th>1</th>\n", | |
" <td>1955</td>\n", | |
" <td>...</td>\n", | |
" <td>584.8</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th>2</th>\n", | |
" <td>1955</td>\n", | |
" <td>...</td>\n", | |
" <td>195.4</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th>3</th>\n", | |
" <td>1955</td>\n", | |
" <td>...</td>\n", | |
" <td>212.6</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th>4</th>\n", | |
" <td>1955</td>\n", | |
" <td>...</td>\n", | |
" <td>19.1</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th>5</th>\n", | |
" <td>1955</td>\n", | |
" <td>...</td>\n", | |
" <td>18.5</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th>6</th>\n", | |
" <td>1955</td>\n", | |
" <td>...</td>\n", | |
" <td>1.6</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th>7</th>\n", | |
" <td>1955</td>\n", | |
" <td>...</td>\n", | |
" <td>182.8</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th>8</th>\n", | |
" <td>1955</td>\n", | |
" <td>...</td>\n", | |
" <td>183.8</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th>9</th>\n", | |
" <td>1955</td>\n", | |
" <td>...</td>\n", | |
" <td>344.4</td>\n", | |
" </tr>\n", | |
" </tbody>\n", | |
"</table>\n", | |
"<p>10 rows × 5 columns</p>\n", | |
"</div>" | |
], | |
"text/plain": [ | |
" Year ... Profit (in millions)\n", | |
"0 1955 ... 806\n", | |
"1 1955 ... 584.8\n", | |
"2 1955 ... 195.4\n", | |
"3 1955 ... 212.6\n", | |
"4 1955 ... 19.1\n", | |
"5 1955 ... 18.5\n", | |
"6 1955 ... 1.6\n", | |
"7 1955 ... 182.8\n", | |
"8 1955 ... 183.8\n", | |
"9 1955 ... 344.4\n", | |
"\n", | |
"[10 rows x 5 columns]" | |
] | |
}, | |
"execution_count": 8, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"df.head(10) #10개 행 보여주기" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 12, | |
"id": "765291f6", | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"pd.set_option(\"display.max_columns\",100)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 13, | |
"id": "983e06fb", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"0 General Motors\n", | |
"1 Exxon Mobil\n", | |
"2 U.S. Steel\n", | |
"3 General Electric\n", | |
"4 Esmark\n", | |
" ... \n", | |
"25495 Wm. Wrigley Jr.\n", | |
"25496 Peabody Energy\n", | |
"25497 Wendy's International\n", | |
"25498 Kindred Healthcare\n", | |
"25499 Cincinnati Financial\n", | |
"Name: Company, Length: 25500, dtype: object" | |
] | |
}, | |
"execution_count": 13, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"df[\"Company\"] # 선택 행만 보기" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 16, | |
"id": "e6f0bbac", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"pandas.core.series.Series" | |
] | |
}, | |
"execution_count": 16, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"type(df[\"Company\"])" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 18, | |
"id": "6f5c16e1", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"ename": "SyntaxError", | |
"evalue": "EOL while scanning string literal (<ipython-input-18-028bc7a19e57>, line 1)", | |
"output_type": "error", | |
"traceback": [ | |
"\u001b[1;36m File \u001b[1;32m\"<ipython-input-18-028bc7a19e57>\"\u001b[1;36m, line \u001b[1;32m1\u001b[0m\n\u001b[1;33m df[\"Rank\",\"Company #안됨. 보고싶은 column 의 list를 제공해야함\u001b[0m\n\u001b[1;37m ^\u001b[0m\n\u001b[1;31mSyntaxError\u001b[0m\u001b[1;31m:\u001b[0m EOL while scanning string literal\n" | |
] | |
} | |
], | |
"source": [ | |
"df[\"Rank\",\"Company ] #안됨. 보고싶은 column 의 list를 제공해야함" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 20, | |
"id": "7ffa9e44", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/html": [ | |
"<div>\n", | |
"<style scoped>\n", | |
" .dataframe tbody tr th:only-of-type {\n", | |
" vertical-align: middle;\n", | |
" }\n", | |
"\n", | |
" .dataframe tbody tr th {\n", | |
" vertical-align: top;\n", | |
" }\n", | |
"\n", | |
" .dataframe thead th {\n", | |
" text-align: right;\n", | |
" }\n", | |
"</style>\n", | |
"<table border=\"1\" class=\"dataframe\">\n", | |
" <thead>\n", | |
" <tr style=\"text-align: right;\">\n", | |
" <th></th>\n", | |
" <th>Rank</th>\n", | |
" <th>Company</th>\n", | |
" </tr>\n", | |
" </thead>\n", | |
" <tbody>\n", | |
" <tr>\n", | |
" <th>0</th>\n", | |
" <td>1</td>\n", | |
" <td>General Motors</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th>1</th>\n", | |
" <td>2</td>\n", | |
" <td>Exxon Mobil</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th>2</th>\n", | |
" <td>3</td>\n", | |
" <td>U.S. Steel</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th>3</th>\n", | |
" <td>4</td>\n", | |
" <td>General Electric</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th>4</th>\n", | |
" <td>5</td>\n", | |
" <td>Esmark</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th>...</th>\n", | |
" <td>...</td>\n", | |
" <td>...</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th>25495</th>\n", | |
" <td>496</td>\n", | |
" <td>Wm. Wrigley Jr.</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th>25496</th>\n", | |
" <td>497</td>\n", | |
" <td>Peabody Energy</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th>25497</th>\n", | |
" <td>498</td>\n", | |
" <td>Wendy's International</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th>25498</th>\n", | |
" <td>499</td>\n", | |
" <td>Kindred Healthcare</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th>25499</th>\n", | |
" <td>500</td>\n", | |
" <td>Cincinnati Financial</td>\n", | |
" </tr>\n", | |
" </tbody>\n", | |
"</table>\n", | |
"<p>25500 rows × 2 columns</p>\n", | |
"</div>" | |
], | |
"text/plain": [ | |
" Rank Company\n", | |
"0 1 General Motors\n", | |
"1 2 Exxon Mobil\n", | |
"2 3 U.S. Steel\n", | |
"3 4 General Electric\n", | |
"4 5 Esmark\n", | |
"... ... ...\n", | |
"25495 496 Wm. Wrigley Jr.\n", | |
"25496 497 Peabody Energy\n", | |
"25497 498 Wendy's International\n", | |
"25498 499 Kindred Healthcare\n", | |
"25499 500 Cincinnati Financial\n", | |
"\n", | |
"[25500 rows x 2 columns]" | |
] | |
}, | |
"execution_count": 20, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"df[[\"Rank\",\"Company\"]] #한개 이상의 데이터 칼럼을 보고싶다면 ㅡ list 로 제공해라" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 21, | |
"id": "bd8053f0", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"pandas.core.frame.DataFrame" | |
] | |
}, | |
"execution_count": 21, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"type(df[[\"Rank\",\"Company\"]] )" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 22, | |
"id": "95ac022a", | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"person= {\n", | |
" \"first\" : \"gildong\",\n", | |
" \"last\" : \"Hong\",\n", | |
" \"email\" : \"ghong@gmail.com\"\n", | |
"} #파이썬의 dictionary 형" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 23, | |
"id": "f715cbdf", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"{'first': 'gildong', 'last': 'Hong', 'email': 'ghong@gmail.com'}" | |
] | |
}, | |
"execution_count": 23, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"person" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 24, | |
"id": "84b5055a", | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"people = {\n", | |
" \"first\" : [\"Gildong\"],\n", | |
" \"last\" : [\"Hone\"],\n", | |
" \"email\" : [\"ghong@gmail.com\"]\n", | |
"}" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 25, | |
"id": "d0bcc8b1", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"{'first': ['Gildong'], 'last': ['Hone'], 'email': ['ghong@gmail.com']}" | |
] | |
}, | |
"execution_count": 25, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"people" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 26, | |
"id": "4d5c4e7f", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"{'first': ['Gildong'], 'last': ['Hone'], 'email': ['ghong@gmail.com']}\n" | |
] | |
} | |
], | |
"source": [ | |
"print(people)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 28, | |
"id": "778b6311", | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"people = {\n", | |
" \"first\" : [\"Gildong\",\"ChunHyang\", \"Mongyrong\"],\n", | |
" \"last\" : [\"Hone\",\"Sung\",\"Lee\"],\n", | |
" \"email\" : [\"ghong@gmail.com\",\"sung@gmail.com\", \"lee@gmail.com\"]\n", | |
"}" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 29, | |
"id": "f0ff5d1e", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"['Gildong', 'ChunHyang', 'Mongyrong']" | |
] | |
}, | |
"execution_count": 29, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"people['first']" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 31, | |
"id": "4c76c741", | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"df = pd.DataFrame(people); #df 새로 만들어 받아오기" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 32, | |
"id": "e43bb7ea", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/html": [ | |
"<div>\n", | |
"<style scoped>\n", | |
" .dataframe tbody tr th:only-of-type {\n", | |
" vertical-align: middle;\n", | |
" }\n", | |
"\n", | |
" .dataframe tbody tr th {\n", | |
" vertical-align: top;\n", | |
" }\n", | |
"\n", | |
" .dataframe thead th {\n", | |
" text-align: right;\n", | |
" }\n", | |
"</style>\n", | |
"<table border=\"1\" class=\"dataframe\">\n", | |
" <thead>\n", | |
" <tr style=\"text-align: right;\">\n", | |
" <th></th>\n", | |
" <th>first</th>\n", | |
" <th>last</th>\n", | |
" <th>email</th>\n", | |
" </tr>\n", | |
" </thead>\n", | |
" <tbody>\n", | |
" <tr>\n", | |
" <th>0</th>\n", | |
" <td>Gildong</td>\n", | |
" <td>Hone</td>\n", | |
" <td>ghong@gmail.com</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th>1</th>\n", | |
" <td>ChunHyang</td>\n", | |
" <td>Sung</td>\n", | |
" <td>sung@gmail.com</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th>2</th>\n", | |
" <td>Mongyrong</td>\n", | |
" <td>Lee</td>\n", | |
" <td>lee@gmail.com</td>\n", | |
" </tr>\n", | |
" </tbody>\n", | |
"</table>\n", | |
"</div>" | |
], | |
"text/plain": [ | |
" first last email\n", | |
"0 Gildong Hone ghong@gmail.com\n", | |
"1 ChunHyang Sung sung@gmail.com\n", | |
"2 Mongyrong Lee lee@gmail.com" | |
] | |
}, | |
"execution_count": 32, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"df" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 33, | |
"id": "7a32f0e0", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"0 ghong@gmail.com\n", | |
"1 sung@gmail.com\n", | |
"2 lee@gmail.com\n", | |
"Name: email, dtype: object" | |
] | |
}, | |
"execution_count": 33, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"df[\"email\"]" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 34, | |
"id": "894ba1f7", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"0 ghong@gmail.com\n", | |
"1 sung@gmail.com\n", | |
"2 lee@gmail.com\n", | |
"Name: email, dtype: object" | |
] | |
}, | |
"execution_count": 34, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"df.email" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 36, | |
"id": "7b273e77", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/html": [ | |
"<div>\n", | |
"<style scoped>\n", | |
" .dataframe tbody tr th:only-of-type {\n", | |
" vertical-align: middle;\n", | |
" }\n", | |
"\n", | |
" .dataframe tbody tr th {\n", | |
" vertical-align: top;\n", | |
" }\n", | |
"\n", | |
" .dataframe thead th {\n", | |
" text-align: right;\n", | |
" }\n", | |
"</style>\n", | |
"<table border=\"1\" class=\"dataframe\">\n", | |
" <thead>\n", | |
" <tr style=\"text-align: right;\">\n", | |
" <th></th>\n", | |
" <th>last</th>\n", | |
" <th>email</th>\n", | |
" </tr>\n", | |
" </thead>\n", | |
" <tbody>\n", | |
" <tr>\n", | |
" <th>0</th>\n", | |
" <td>Hone</td>\n", | |
" <td>ghong@gmail.com</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th>1</th>\n", | |
" <td>Sung</td>\n", | |
" <td>sung@gmail.com</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th>2</th>\n", | |
" <td>Lee</td>\n", | |
" <td>lee@gmail.com</td>\n", | |
" </tr>\n", | |
" </tbody>\n", | |
"</table>\n", | |
"</div>" | |
], | |
"text/plain": [ | |
" last email\n", | |
"0 Hone ghong@gmail.com\n", | |
"1 Sung sung@gmail.com\n", | |
"2 Lee lee@gmail.com" | |
] | |
}, | |
"execution_count": 36, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"df[[\"last\",\"email\"]]" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 37, | |
"id": "baaaafb5", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"Index(['first', 'last', 'email'], dtype='object')" | |
] | |
}, | |
"execution_count": 37, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"df.columns #문자 타입은 데이터타입 object 로 나온다??" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 38, | |
"id": "409a633b", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"0 Hone\n", | |
"1 Sung\n", | |
"2 Lee\n", | |
"Name: last, dtype: object" | |
] | |
}, | |
"execution_count": 38, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"df[\"last\"].head(3)" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"id": "e2dd5d2b", | |
"metadata": {}, | |
"source": [ | |
"__loc__, __iloc__ #를 사용하여 특정 하여 특정 행을 인출 할 수 있다 \n" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 40, | |
"id": "1259a94f", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"first Gildong\n", | |
"last Hone\n", | |
"email ghong@gmail.com\n", | |
"Name: 0, dtype: object" | |
] | |
}, | |
"execution_count": 40, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"df.iloc[0] #첫번째행만 뽑기" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 41, | |
"id": "7e3275fc", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"'Hone'" | |
] | |
}, | |
"execution_count": 41, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"df.iloc[0,1] # 1번행의 2번열 ㅋㅋ" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 42, | |
"id": "c86cc54b", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/html": [ | |
"<div>\n", | |
"<style scoped>\n", | |
" .dataframe tbody tr th:only-of-type {\n", | |
" vertical-align: middle;\n", | |
" }\n", | |
"\n", | |
" .dataframe tbody tr th {\n", | |
" vertical-align: top;\n", | |
" }\n", | |
"\n", | |
" .dataframe thead th {\n", | |
" text-align: right;\n", | |
" }\n", | |
"</style>\n", | |
"<table border=\"1\" class=\"dataframe\">\n", | |
" <thead>\n", | |
" <tr style=\"text-align: right;\">\n", | |
" <th></th>\n", | |
" <th>first</th>\n", | |
" <th>last</th>\n", | |
" <th>email</th>\n", | |
" </tr>\n", | |
" </thead>\n", | |
" <tbody>\n", | |
" <tr>\n", | |
" <th>0</th>\n", | |
" <td>Gildong</td>\n", | |
" <td>Hone</td>\n", | |
" <td>ghong@gmail.com</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th>1</th>\n", | |
" <td>ChunHyang</td>\n", | |
" <td>Sung</td>\n", | |
" <td>sung@gmail.com</td>\n", | |
" </tr>\n", | |
" </tbody>\n", | |
"</table>\n", | |
"</div>" | |
], | |
"text/plain": [ | |
" first last email\n", | |
"0 Gildong Hone ghong@gmail.com\n", | |
"1 ChunHyang Sung sung@gmail.com" | |
] | |
}, | |
"execution_count": 42, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"df.iloc[[0,1]] #0번행과 1번행" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 45, | |
"id": "8e1faadf", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"0 ghong@gmail.com\n", | |
"1 sung@gmail.com\n", | |
"Name: email, dtype: object" | |
] | |
}, | |
"execution_count": 45, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"df.iloc[[0,1],2] #두번째 인자는 , 첫번째는 행 두번째는 칼럼\n", | |
" #iloc 은 위치기반... 0번부터" | |
] | |
} | |
], | |
"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 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
my memo :: https://habbang0.tistory.com/75