-
-
Save olivx/64d657ff67ecfcc38917f5f34cc1495b to your computer and use it in GitHub Desktop.
Annotations of Pandas DataFrame
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": 1, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"import pandas as pd" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 2, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"'\\nCrie uma venv com Python3\\npip install jupyter pandas xlrd xlwt openpyxl\\n\\nRode o jupyter\\n$ jupyter notebook\\n'" | |
] | |
}, | |
"execution_count": 2, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"'''\n", | |
"Crie uma venv com Python3\n", | |
"pip install jupyter pandas xlrd xlwt openpyxl\n", | |
"\n", | |
"Rode o jupyter\n", | |
"$ jupyter notebook\n", | |
"'''" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 3, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"filename = '/home/dev/Downloads/escola.xlsx'" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 4, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"df = pd.read_excel(filename)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 5, | |
"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>Escola</th>\n", | |
" <th>Aluno</th>\n", | |
" <th>email_</th>\n", | |
" <th>Pai</th>\n", | |
" <th>Mae</th>\n", | |
" <th>Responsavel</th>\n", | |
" </tr>\n", | |
" </thead>\n", | |
" <tbody>\n", | |
" <tr>\n", | |
" <th>0</th>\n", | |
" <td>Escola 1</td>\n", | |
" <td>Aluno1</td>\n", | |
" <td>[email protected]</td>\n", | |
" <td>Nome Pai_Aluno1</td>\n", | |
" <td>Nome Mae_Aluno1</td>\n", | |
" <td>Responsavel_Aluno1</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th>1</th>\n", | |
" <td>Escola 1</td>\n", | |
" <td>Aluno2</td>\n", | |
" <td>[email protected];[email protected]</td>\n", | |
" <td>Nome Pai_Aluno2</td>\n", | |
" <td>Nome Mae_Aluno2</td>\n", | |
" <td>Responsavel_Aluno2</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th>2</th>\n", | |
" <td>Escola 1</td>\n", | |
" <td>Aluno3</td>\n", | |
" <td>[email protected];[email protected];[email protected]</td>\n", | |
" <td>Nome Pai_Aluno3</td>\n", | |
" <td>Nome Mae_Aluno3</td>\n", | |
" <td>Responsavel_Aluno3</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th>3</th>\n", | |
" <td>Escola 1</td>\n", | |
" <td>Aluno4</td>\n", | |
" <td>[email protected];[email protected];[email protected]...</td>\n", | |
" <td>Nome Pai_Aluno4</td>\n", | |
" <td>Nome Mae_Aluno4</td>\n", | |
" <td>Responsavel_Aluno4</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th>4</th>\n", | |
" <td>Escola 1</td>\n", | |
" <td>Aluno5</td>\n", | |
" <td>[email protected];[email protected];[email protected]...</td>\n", | |
" <td>Nome Pai_Aluno5</td>\n", | |
" <td>Nome Mae_Aluno5</td>\n", | |
" <td>Responsavel_Aluno5</td>\n", | |
" </tr>\n", | |
" </tbody>\n", | |
"</table>\n", | |
"</div>" | |
], | |
"text/plain": [ | |
" Escola Aluno email_ \\\n", | |
"0 Escola 1 Aluno1 [email protected] \n", | |
"1 Escola 1 Aluno2 [email protected];[email protected] \n", | |
"2 Escola 1 Aluno3 [email protected];[email protected];[email protected] \n", | |
"3 Escola 1 Aluno4 [email protected];[email protected];[email protected]... \n", | |
"4 Escola 1 Aluno5 [email protected];[email protected];[email protected]... \n", | |
"\n", | |
" Pai Mae Responsavel \n", | |
"0 Nome Pai_Aluno1 Nome Mae_Aluno1 Responsavel_Aluno1 \n", | |
"1 Nome Pai_Aluno2 Nome Mae_Aluno2 Responsavel_Aluno2 \n", | |
"2 Nome Pai_Aluno3 Nome Mae_Aluno3 Responsavel_Aluno3 \n", | |
"3 Nome Pai_Aluno4 Nome Mae_Aluno4 Responsavel_Aluno4 \n", | |
"4 Nome Pai_Aluno5 Nome Mae_Aluno5 Responsavel_Aluno5 " | |
] | |
}, | |
"execution_count": 5, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"df" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 6, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"new_escola = []\n", | |
"new_email = []\n", | |
"new_aluno = []\n", | |
"new_pai = []\n", | |
"new_mae = []\n", | |
"new_responsavel = []" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 7, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"for row in df.itertuples():\n", | |
" if len(row.email_.split(';')) > 1:\n", | |
" for index in row.email_.split(';'):\n", | |
" new_escola.append(row.Escola)\n", | |
" new_email.append(index)\n", | |
" new_aluno.append(row.Aluno)\n", | |
" new_pai.append(row.Pai)\n", | |
" new_mae.append(row.Mae)\n", | |
" new_responsavel.append(row.Responsavel)\n", | |
" else:\n", | |
" new_escola.append(row.Escola)\n", | |
" new_email.append(row.email_)\n", | |
" new_aluno.append(row.Aluno)\n", | |
" new_pai.append(row.Pai)\n", | |
" new_mae.append(row.Mae)\n", | |
" new_responsavel.append(row.Responsavel)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 8, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"dffinal = pd.DataFrame({\n", | |
" 'new_escola': new_escola,\n", | |
" 'new_email': new_email,\n", | |
" 'new_aluno': new_aluno,\n", | |
" 'new_pai': new_pai,\n", | |
" 'new_mae': new_mae,\n", | |
" 'new_responsavel': new_responsavel,\n", | |
"})" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 9, | |
"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>new_aluno</th>\n", | |
" <th>new_email</th>\n", | |
" <th>new_escola</th>\n", | |
" <th>new_mae</th>\n", | |
" <th>new_pai</th>\n", | |
" <th>new_responsavel</th>\n", | |
" </tr>\n", | |
" </thead>\n", | |
" <tbody>\n", | |
" <tr>\n", | |
" <th>0</th>\n", | |
" <td>Aluno1</td>\n", | |
" <td>[email protected]</td>\n", | |
" <td>Escola 1</td>\n", | |
" <td>Nome Mae_Aluno1</td>\n", | |
" <td>Nome Pai_Aluno1</td>\n", | |
" <td>Responsavel_Aluno1</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th>1</th>\n", | |
" <td>Aluno2</td>\n", | |
" <td>[email protected]</td>\n", | |
" <td>Escola 1</td>\n", | |
" <td>Nome Mae_Aluno2</td>\n", | |
" <td>Nome Pai_Aluno2</td>\n", | |
" <td>Responsavel_Aluno2</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th>2</th>\n", | |
" <td>Aluno2</td>\n", | |
" <td>[email protected]</td>\n", | |
" <td>Escola 1</td>\n", | |
" <td>Nome Mae_Aluno2</td>\n", | |
" <td>Nome Pai_Aluno2</td>\n", | |
" <td>Responsavel_Aluno2</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th>3</th>\n", | |
" <td>Aluno3</td>\n", | |
" <td>[email protected]</td>\n", | |
" <td>Escola 1</td>\n", | |
" <td>Nome Mae_Aluno3</td>\n", | |
" <td>Nome Pai_Aluno3</td>\n", | |
" <td>Responsavel_Aluno3</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th>4</th>\n", | |
" <td>Aluno3</td>\n", | |
" <td>[email protected]</td>\n", | |
" <td>Escola 1</td>\n", | |
" <td>Nome Mae_Aluno3</td>\n", | |
" <td>Nome Pai_Aluno3</td>\n", | |
" <td>Responsavel_Aluno3</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th>5</th>\n", | |
" <td>Aluno3</td>\n", | |
" <td>[email protected]</td>\n", | |
" <td>Escola 1</td>\n", | |
" <td>Nome Mae_Aluno3</td>\n", | |
" <td>Nome Pai_Aluno3</td>\n", | |
" <td>Responsavel_Aluno3</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th>6</th>\n", | |
" <td>Aluno4</td>\n", | |
" <td>[email protected]</td>\n", | |
" <td>Escola 1</td>\n", | |
" <td>Nome Mae_Aluno4</td>\n", | |
" <td>Nome Pai_Aluno4</td>\n", | |
" <td>Responsavel_Aluno4</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th>7</th>\n", | |
" <td>Aluno4</td>\n", | |
" <td>[email protected]</td>\n", | |
" <td>Escola 1</td>\n", | |
" <td>Nome Mae_Aluno4</td>\n", | |
" <td>Nome Pai_Aluno4</td>\n", | |
" <td>Responsavel_Aluno4</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th>8</th>\n", | |
" <td>Aluno4</td>\n", | |
" <td>[email protected]</td>\n", | |
" <td>Escola 1</td>\n", | |
" <td>Nome Mae_Aluno4</td>\n", | |
" <td>Nome Pai_Aluno4</td>\n", | |
" <td>Responsavel_Aluno4</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th>9</th>\n", | |
" <td>Aluno4</td>\n", | |
" <td>[email protected]</td>\n", | |
" <td>Escola 1</td>\n", | |
" <td>Nome Mae_Aluno4</td>\n", | |
" <td>Nome Pai_Aluno4</td>\n", | |
" <td>Responsavel_Aluno4</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th>10</th>\n", | |
" <td>Aluno5</td>\n", | |
" <td>[email protected]</td>\n", | |
" <td>Escola 1</td>\n", | |
" <td>Nome Mae_Aluno5</td>\n", | |
" <td>Nome Pai_Aluno5</td>\n", | |
" <td>Responsavel_Aluno5</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th>11</th>\n", | |
" <td>Aluno5</td>\n", | |
" <td>[email protected]</td>\n", | |
" <td>Escola 1</td>\n", | |
" <td>Nome Mae_Aluno5</td>\n", | |
" <td>Nome Pai_Aluno5</td>\n", | |
" <td>Responsavel_Aluno5</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th>12</th>\n", | |
" <td>Aluno5</td>\n", | |
" <td>[email protected]</td>\n", | |
" <td>Escola 1</td>\n", | |
" <td>Nome Mae_Aluno5</td>\n", | |
" <td>Nome Pai_Aluno5</td>\n", | |
" <td>Responsavel_Aluno5</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th>13</th>\n", | |
" <td>Aluno5</td>\n", | |
" <td>[email protected]</td>\n", | |
" <td>Escola 1</td>\n", | |
" <td>Nome Mae_Aluno5</td>\n", | |
" <td>Nome Pai_Aluno5</td>\n", | |
" <td>Responsavel_Aluno5</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th>14</th>\n", | |
" <td>Aluno5</td>\n", | |
" <td>[email protected]</td>\n", | |
" <td>Escola 1</td>\n", | |
" <td>Nome Mae_Aluno5</td>\n", | |
" <td>Nome Pai_Aluno5</td>\n", | |
" <td>Responsavel_Aluno5</td>\n", | |
" </tr>\n", | |
" </tbody>\n", | |
"</table>\n", | |
"</div>" | |
], | |
"text/plain": [ | |
" new_aluno new_email new_escola new_mae new_pai \\\n", | |
"0 Aluno1 [email protected] Escola 1 Nome Mae_Aluno1 Nome Pai_Aluno1 \n", | |
"1 Aluno2 [email protected] Escola 1 Nome Mae_Aluno2 Nome Pai_Aluno2 \n", | |
"2 Aluno2 [email protected] Escola 1 Nome Mae_Aluno2 Nome Pai_Aluno2 \n", | |
"3 Aluno3 [email protected] Escola 1 Nome Mae_Aluno3 Nome Pai_Aluno3 \n", | |
"4 Aluno3 [email protected] Escola 1 Nome Mae_Aluno3 Nome Pai_Aluno3 \n", | |
"5 Aluno3 [email protected] Escola 1 Nome Mae_Aluno3 Nome Pai_Aluno3 \n", | |
"6 Aluno4 [email protected] Escola 1 Nome Mae_Aluno4 Nome Pai_Aluno4 \n", | |
"7 Aluno4 [email protected] Escola 1 Nome Mae_Aluno4 Nome Pai_Aluno4 \n", | |
"8 Aluno4 [email protected] Escola 1 Nome Mae_Aluno4 Nome Pai_Aluno4 \n", | |
"9 Aluno4 [email protected] Escola 1 Nome Mae_Aluno4 Nome Pai_Aluno4 \n", | |
"10 Aluno5 [email protected] Escola 1 Nome Mae_Aluno5 Nome Pai_Aluno5 \n", | |
"11 Aluno5 [email protected] Escola 1 Nome Mae_Aluno5 Nome Pai_Aluno5 \n", | |
"12 Aluno5 [email protected] Escola 1 Nome Mae_Aluno5 Nome Pai_Aluno5 \n", | |
"13 Aluno5 [email protected] Escola 1 Nome Mae_Aluno5 Nome Pai_Aluno5 \n", | |
"14 Aluno5 [email protected] Escola 1 Nome Mae_Aluno5 Nome Pai_Aluno5 \n", | |
"\n", | |
" new_responsavel \n", | |
"0 Responsavel_Aluno1 \n", | |
"1 Responsavel_Aluno2 \n", | |
"2 Responsavel_Aluno2 \n", | |
"3 Responsavel_Aluno3 \n", | |
"4 Responsavel_Aluno3 \n", | |
"5 Responsavel_Aluno3 \n", | |
"6 Responsavel_Aluno4 \n", | |
"7 Responsavel_Aluno4 \n", | |
"8 Responsavel_Aluno4 \n", | |
"9 Responsavel_Aluno4 \n", | |
"10 Responsavel_Aluno5 \n", | |
"11 Responsavel_Aluno5 \n", | |
"12 Responsavel_Aluno5 \n", | |
"13 Responsavel_Aluno5 \n", | |
"14 Responsavel_Aluno5 " | |
] | |
}, | |
"execution_count": 9, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"dffinal" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 10, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"dffinal.to_excel('/home/dev/Downloads/escola_final.xlsx')" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"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.5.2" | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 2 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment