{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# <font color='blue'>Data Science Academy - Python Fundamentos - Capítulo 8</font>\n",
    "\n",
    "## Download: http://github.com/dsacademybr"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Exercícios Sobre Módulos Python Para Análise de Dados"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### ****** ATENÇÃO ******\n",
    "### Alguns dos exercícios podem requerer pesquisa adicional na documentação dos pacotes. Pesquise!"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Exercício 1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "CPU times: user 13.5 ms, sys: 4.26 ms, total: 17.8 ms\n",
      "Wall time: 17.9 ms\n",
      "CPU times: user 587 ms, sys: 169 ms, total: 756 ms\n",
      "Wall time: 757 ms\n"
     ]
    }
   ],
   "source": [
    "# Crie um array NumPy com 1000000 e uma lista com 1000000.\n",
    "# Multiplique cada elemento do array e da lista por 2 e calcule o tempo de execução com cada um dos objetos (use %time).\n",
    "# Qual objeto oferece melhor performance, array NumPy ou lista?\n",
    "import numpy as np\n",
    "my_arr = np.arange(1000000)\n",
    "my_list = list(range(1000000))\n",
    "\n",
    "%time for _ in range(10): my_arr2 = my_arr * 2\n",
    "%time for _ in range(10): my_list2 = [x * 2 for x in my_list]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Exercício 2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Exercício 2\n",
    "# Crie um array de 10 elementos\n",
    "# Altere o valores de todos os elementos dos índices 5 a 8 para 0\n",
    "import numpy as np\n",
    "arr = np.arange(10)\n",
    "arr"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([0, 1, 2, 3, 4, 0, 0, 0, 8, 9])"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr[5:8] = 0\n",
    "arr"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Exercício 3"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[[ 1,  2,  3],\n",
       "        [ 4,  5,  6]],\n",
       "\n",
       "       [[ 7,  8,  9],\n",
       "        [10, 11, 12]]])"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Crie um array de 3 dimensões e imprima a dimensão 1 \n",
    "import numpy as np\n",
    "arr3d = np.array([[[1, 2, 3], [4, 5, 6]], [[7, 8, 9], [10, 11, 12]]])\n",
    "arr3d"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1, 2, 3],\n",
       "       [4, 5, 6]])"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr3d[0]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Exercício 4"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1, 2, 3],\n",
       "       [4, 5, 6],\n",
       "       [7, 8, 9]])"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Crie um array de duas dimensões (matriz).\n",
    "# Imprima os elementos da terceira linha da matriz\n",
    "# Imprima todos os elementos da primeira e segunda linhas e segunda e terceira colunas\n",
    "arr2d = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])\n",
    "arr2d"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([7, 8, 9])"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Imprima os elementos da terceira linha da matriz\n",
    "arr2d[2]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[2, 3],\n",
       "       [5, 6]])"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Imprima todos os elementos da primeira e segunda linhas e segunda e terceira colunas\n",
    "arr2d[:2, 1:]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Exercício 5"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 0,  1,  2,  3,  4],\n",
       "       [ 5,  6,  7,  8,  9],\n",
       "       [10, 11, 12, 13, 14]])"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Calcule a transposta da matriz abaixo\n",
    "arr = np.arange(15).reshape((3, 5))\n",
    "arr"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 0,  5, 10],\n",
       "       [ 1,  6, 11],\n",
       "       [ 2,  7, 12],\n",
       "       [ 3,  8, 13],\n",
       "       [ 4,  9, 14]])"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr.T"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Exercício 6"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# Considere os 3 arrays abaixo\n",
    "# Retorne o valor do array xarr se o valor for True no array cond. Caso contrário, retorne o valor do array yarr.\n",
    "xarr = np.array([1.1, 1.2, 1.3, 1.4, 1.5])\n",
    "yarr = np.array([2.1, 2.2, 2.3, 2.4, 2.5])\n",
    "cond = np.array([True, False, True, True, False])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1.1 2.1 True\n",
      "1.2 2.2 False\n",
      "1.3 2.3 True\n",
      "1.4 2.4 True\n",
      "1.5 2.5 False\n"
     ]
    }
   ],
   "source": [
    "for x, y, c in zip(xarr, yarr, cond):\n",
    "    print(x, y, c)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[1.1, 2.2, 1.3, 1.4, 2.5]"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "resultado = [(x if c else y) for x, y, c in zip(xarr, yarr, cond)]\n",
    "resultado"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Exercício 7"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[0 1 2 3 4 5 6 7 8 9]\n"
     ]
    }
   ],
   "source": [
    "# Crie um array A com 10 elementos e salve o array em disco com a extensão npy\n",
    "# Depois carregue o array do disco no array B\n",
    "A = np.arange(10)\n",
    "print(A)\n",
    "np.save('array_a', A)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[0 1 2 3 4 5 6 7 8 9]\n"
     ]
    }
   ],
   "source": [
    "B = np.load('array_a.npy')\n",
    "print(B)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": true
   },
   "source": [
    "### Exercício 8"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# Considerando a série abaixo, imprima os valores únicos na série\n",
    "import pandas as pd\n",
    "obj = pd.Series(['c', 'a', 'd', 'a', 'a', 'b', 'b', 'c', 'c', 'a', 'b'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array(['c', 'a', 'd', 'b'], dtype=object)"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "uniques = obj.unique()\n",
    "uniques"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": true
   },
   "source": [
    "### Exercício 9"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<Response [200]>"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Considerando o trecho de código que conecta em uma url na internet, imprima o dataframe conforme abaixo.\n",
    "import requests\n",
    "url = 'https://api.github.com/repos/pandas-dev/pandas/issues'\n",
    "resp = requests.get(url)\n",
    "resp"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'Slicing columns with mixed types <str>,<int> fails with ValueError'"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data = resp.json()\n",
    "data[0]['title']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "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>number</th>\n",
       "      <th>title</th>\n",
       "      <th>labels</th>\n",
       "      <th>state</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>20975</td>\n",
       "      <td>Slicing columns with mixed types &lt;str&gt;,&lt;int&gt; f...</td>\n",
       "      <td>[]</td>\n",
       "      <td>open</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>20974</td>\n",
       "      <td>BUG: Fix drop_duplicates failure when DataFram...</td>\n",
       "      <td>[{'id': 76811, 'url': 'https://api.github.com/...</td>\n",
       "      <td>open</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>20970</td>\n",
       "      <td>performance when \"combining\" categories</td>\n",
       "      <td>[{'id': 78527356, 'url': 'https://api.github.c...</td>\n",
       "      <td>open</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>20969</td>\n",
       "      <td>Getting a ... in my CSV when using to_csv()</td>\n",
       "      <td>[]</td>\n",
       "      <td>open</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>20968</td>\n",
       "      <td>Sharey keyword for boxplot</td>\n",
       "      <td>[]</td>\n",
       "      <td>open</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>20967</td>\n",
       "      <td>Column can be extended beyond DataFrame's length</td>\n",
       "      <td>[]</td>\n",
       "      <td>open</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>20966</td>\n",
       "      <td>BUG: Fix wrong khash method definition</td>\n",
       "      <td>[{'id': 76811, 'url': 'https://api.github.com/...</td>\n",
       "      <td>open</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>20965</td>\n",
       "      <td>BUG: Fix combine_first converts other columns ...</td>\n",
       "      <td>[{'id': 76811, 'url': 'https://api.github.com/...</td>\n",
       "      <td>open</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>20964</td>\n",
       "      <td>BUG: Index with integer data and datetime64[ns...</td>\n",
       "      <td>[]</td>\n",
       "      <td>open</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>20962</td>\n",
       "      <td>df.rank() has different behavior for python2 a...</td>\n",
       "      <td>[]</td>\n",
       "      <td>open</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>10</th>\n",
       "      <td>20960</td>\n",
       "      <td>DOC: add reshaping visuals to the docs (Reshap...</td>\n",
       "      <td>[{'id': 134699, 'url': 'https://api.github.com...</td>\n",
       "      <td>open</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>11</th>\n",
       "      <td>20958</td>\n",
       "      <td>Using apply on a grouper works only if done af...</td>\n",
       "      <td>[{'id': 76811, 'url': 'https://api.github.com/...</td>\n",
       "      <td>open</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>12</th>\n",
       "      <td>20956</td>\n",
       "      <td>ENH: Return DatetimeIndex or TimedeltaIndex bi...</td>\n",
       "      <td>[{'id': 49597148, 'url': 'https://api.github.c...</td>\n",
       "      <td>open</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>13</th>\n",
       "      <td>20955</td>\n",
       "      <td>Building documentation fails with `ImportError...</td>\n",
       "      <td>[]</td>\n",
       "      <td>open</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>14</th>\n",
       "      <td>20954</td>\n",
       "      <td>Correlation inconsistencies between Series and...</td>\n",
       "      <td>[]</td>\n",
       "      <td>open</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>15</th>\n",
       "      <td>20951</td>\n",
       "      <td>Addressing multiindex raises TypeError if indi...</td>\n",
       "      <td>[]</td>\n",
       "      <td>open</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>16</th>\n",
       "      <td>20948</td>\n",
       "      <td>Indexing DataFrame with DateOffset is nearly i...</td>\n",
       "      <td>[]</td>\n",
       "      <td>open</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>17</th>\n",
       "      <td>20947</td>\n",
       "      <td>Allow drop bins when using the cut function</td>\n",
       "      <td>[{'id': 76812, 'url': 'https://api.github.com/...</td>\n",
       "      <td>open</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>18</th>\n",
       "      <td>20945</td>\n",
       "      <td>Data is mismatched with labels after stack wit...</td>\n",
       "      <td>[]</td>\n",
       "      <td>open</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>19</th>\n",
       "      <td>20944</td>\n",
       "      <td>Drop rows based on condition</td>\n",
       "      <td>[]</td>\n",
       "      <td>open</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>20</th>\n",
       "      <td>20943</td>\n",
       "      <td>pd.read_sql does not handle queries that retur...</td>\n",
       "      <td>[]</td>\n",
       "      <td>open</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>21</th>\n",
       "      <td>20940</td>\n",
       "      <td>unclear error with  read_sas</td>\n",
       "      <td>[]</td>\n",
       "      <td>open</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>22</th>\n",
       "      <td>20937</td>\n",
       "      <td>BUG: Series(DTI/TDI) loses the frequency infor...</td>\n",
       "      <td>[]</td>\n",
       "      <td>open</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>23</th>\n",
       "      <td>20936</td>\n",
       "      <td>__new__() missing 1 required positional argume...</td>\n",
       "      <td>[]</td>\n",
       "      <td>open</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>24</th>\n",
       "      <td>20932</td>\n",
       "      <td>API: expose `axis` keyword in pandas.core.algo...</td>\n",
       "      <td>[{'id': 35818298, 'url': 'https://api.github.c...</td>\n",
       "      <td>open</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>25</th>\n",
       "      <td>20930</td>\n",
       "      <td>DOC: Index.get_loc Cannot Accept List-like Tol...</td>\n",
       "      <td>[]</td>\n",
       "      <td>open</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>26</th>\n",
       "      <td>20928</td>\n",
       "      <td>Added script to fetch wheels [ci skip]</td>\n",
       "      <td>[{'id': 131473665, 'url': 'https://api.github....</td>\n",
       "      <td>open</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>27</th>\n",
       "      <td>20927</td>\n",
       "      <td>read_sas OverflowError: int too big to convert...</td>\n",
       "      <td>[]</td>\n",
       "      <td>open</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>28</th>\n",
       "      <td>20926</td>\n",
       "      <td>BLD: Lint for invalid files in wheels / packages</td>\n",
       "      <td>[{'id': 129350, 'url': 'https://api.github.com...</td>\n",
       "      <td>open</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>29</th>\n",
       "      <td>20925</td>\n",
       "      <td>Series.reset_index(level_name, drop=True) acce...</td>\n",
       "      <td>[{'id': 42670965, 'url': 'https://api.github.c...</td>\n",
       "      <td>open</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "    number                                              title  \\\n",
       "0    20975  Slicing columns with mixed types <str>,<int> f...   \n",
       "1    20974  BUG: Fix drop_duplicates failure when DataFram...   \n",
       "2    20970            performance when \"combining\" categories   \n",
       "3    20969        Getting a ... in my CSV when using to_csv()   \n",
       "4    20968                         Sharey keyword for boxplot   \n",
       "5    20967   Column can be extended beyond DataFrame's length   \n",
       "6    20966             BUG: Fix wrong khash method definition   \n",
       "7    20965  BUG: Fix combine_first converts other columns ...   \n",
       "8    20964  BUG: Index with integer data and datetime64[ns...   \n",
       "9    20962  df.rank() has different behavior for python2 a...   \n",
       "10   20960  DOC: add reshaping visuals to the docs (Reshap...   \n",
       "11   20958  Using apply on a grouper works only if done af...   \n",
       "12   20956  ENH: Return DatetimeIndex or TimedeltaIndex bi...   \n",
       "13   20955  Building documentation fails with `ImportError...   \n",
       "14   20954  Correlation inconsistencies between Series and...   \n",
       "15   20951  Addressing multiindex raises TypeError if indi...   \n",
       "16   20948  Indexing DataFrame with DateOffset is nearly i...   \n",
       "17   20947        Allow drop bins when using the cut function   \n",
       "18   20945  Data is mismatched with labels after stack wit...   \n",
       "19   20944                       Drop rows based on condition   \n",
       "20   20943  pd.read_sql does not handle queries that retur...   \n",
       "21   20940                       unclear error with  read_sas   \n",
       "22   20937  BUG: Series(DTI/TDI) loses the frequency infor...   \n",
       "23   20936  __new__() missing 1 required positional argume...   \n",
       "24   20932  API: expose `axis` keyword in pandas.core.algo...   \n",
       "25   20930  DOC: Index.get_loc Cannot Accept List-like Tol...   \n",
       "26   20928             Added script to fetch wheels [ci skip]   \n",
       "27   20927  read_sas OverflowError: int too big to convert...   \n",
       "28   20926   BLD: Lint for invalid files in wheels / packages   \n",
       "29   20925  Series.reset_index(level_name, drop=True) acce...   \n",
       "\n",
       "                                               labels state  \n",
       "0                                                  []  open  \n",
       "1   [{'id': 76811, 'url': 'https://api.github.com/...  open  \n",
       "2   [{'id': 78527356, 'url': 'https://api.github.c...  open  \n",
       "3                                                  []  open  \n",
       "4                                                  []  open  \n",
       "5                                                  []  open  \n",
       "6   [{'id': 76811, 'url': 'https://api.github.com/...  open  \n",
       "7   [{'id': 76811, 'url': 'https://api.github.com/...  open  \n",
       "8                                                  []  open  \n",
       "9                                                  []  open  \n",
       "10  [{'id': 134699, 'url': 'https://api.github.com...  open  \n",
       "11  [{'id': 76811, 'url': 'https://api.github.com/...  open  \n",
       "12  [{'id': 49597148, 'url': 'https://api.github.c...  open  \n",
       "13                                                 []  open  \n",
       "14                                                 []  open  \n",
       "15                                                 []  open  \n",
       "16                                                 []  open  \n",
       "17  [{'id': 76812, 'url': 'https://api.github.com/...  open  \n",
       "18                                                 []  open  \n",
       "19                                                 []  open  \n",
       "20                                                 []  open  \n",
       "21                                                 []  open  \n",
       "22                                                 []  open  \n",
       "23                                                 []  open  \n",
       "24  [{'id': 35818298, 'url': 'https://api.github.c...  open  \n",
       "25                                                 []  open  \n",
       "26  [{'id': 131473665, 'url': 'https://api.github....  open  \n",
       "27                                                 []  open  \n",
       "28  [{'id': 129350, 'url': 'https://api.github.com...  open  \n",
       "29  [{'id': 42670965, 'url': 'https://api.github.c...  open  "
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "issues = pd.DataFrame(data, columns=['number', 'title', 'labels', 'state'])\n",
    "issues"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Exercício 10"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# Crie um banco de dados no SQLite, crie uma tabela, insira registros, \n",
    "# consulte a tabela e retorne os dados em dataframe do Pandas\n",
    "import sqlite3\n",
    "import pandas as pd\n",
    "query = \"\"\"\n",
    "CREATE TABLE TESTE\n",
    "(Cidade VARCHAR(20), \n",
    " Estado VARCHAR(20),\n",
    " taxa REAL,        \n",
    " Impostos INTEGER\n",
    ");\"\"\"\n",
    "con = sqlite3.connect('dsa.db')\n",
    "con.execute(query)\n",
    "con.commit()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "data = [('Natal', 'Rio Grande do Norte', 1.25, 6),\n",
    "        ('Recife', 'Pernambuco', 2.6, 3),\n",
    "        ('Londrina', 'Paraná', 1.7, 5)]\n",
    "stmt = \"INSERT INTO TESTE VALUES(?, ?, ?, ?)\"\n",
    "con.executemany(stmt, data)\n",
    "con.commit()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[('Natal', 'Rio Grande do Norte', 1.25, 6),\n",
       " ('Recife', 'Pernambuco', 2.6, 3),\n",
       " ('Londrina', 'Paraná', 1.7, 5)]"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "cursor = con.execute('select * from teste')\n",
    "rows = cursor.fetchall()\n",
    "rows"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "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>Cidade</th>\n",
       "      <th>Estado</th>\n",
       "      <th>taxa</th>\n",
       "      <th>Impostos</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>Natal</td>\n",
       "      <td>Rio Grande do Norte</td>\n",
       "      <td>1.25</td>\n",
       "      <td>6</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>Recife</td>\n",
       "      <td>Pernambuco</td>\n",
       "      <td>2.60</td>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>Londrina</td>\n",
       "      <td>Paraná</td>\n",
       "      <td>1.70</td>\n",
       "      <td>5</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "     Cidade               Estado  taxa  Impostos\n",
       "0     Natal  Rio Grande do Norte  1.25         6\n",
       "1    Recife           Pernambuco  2.60         3\n",
       "2  Londrina               Paraná  1.70         5"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "cursor.description\n",
    "pd.DataFrame(rows, columns=[x[0] for x in cursor.description])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Fim"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Obrigado - Data Science Academy - <a href=\"http://facebook.com/dsacademybr\">facebook.com/dsacademybr</a>"
   ]
  }
 ],
 "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.6.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
