-
-
Save rg3915/5fb3a2e7338115bc92e82b7a9a2b372b to your computer and use it in GitHub Desktop.
| { | |
| "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>pop1@pop.com.br</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>pop1@pop.com.br;pop2@pop.com.br</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>pop1@pop.com.br;pop2@pop.com.br;pop3@pop.com.br</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>pop1@pop.com.br;pop2@pop.com.br;pop3@pop.com.b...</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>pop1@pop.com.br;pop2@pop.com.br;pop3@pop.com.b...</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 pop1@pop.com.br \n", | |
| "1 Escola 1 Aluno2 pop1@pop.com.br;pop2@pop.com.br \n", | |
| "2 Escola 1 Aluno3 pop1@pop.com.br;pop2@pop.com.br;pop3@pop.com.br \n", | |
| "3 Escola 1 Aluno4 pop1@pop.com.br;pop2@pop.com.br;pop3@pop.com.b... \n", | |
| "4 Escola 1 Aluno5 pop1@pop.com.br;pop2@pop.com.br;pop3@pop.com.b... \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>pop1@pop.com.br</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>pop1@pop.com.br</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>pop2@pop.com.br</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>pop1@pop.com.br</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>pop2@pop.com.br</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>pop3@pop.com.br</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>pop1@pop.com.br</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>pop2@pop.com.br</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>pop3@pop.com.br</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>pop4@pop.com.br</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>pop1@pop.com.br</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>pop2@pop.com.br</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>pop3@pop.com.br</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>pop4@pop.com.br</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>pop5@pop.com.br</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 pop1@pop.com.br Escola 1 Nome Mae_Aluno1 Nome Pai_Aluno1 \n", | |
| "1 Aluno2 pop1@pop.com.br Escola 1 Nome Mae_Aluno2 Nome Pai_Aluno2 \n", | |
| "2 Aluno2 pop2@pop.com.br Escola 1 Nome Mae_Aluno2 Nome Pai_Aluno2 \n", | |
| "3 Aluno3 pop1@pop.com.br Escola 1 Nome Mae_Aluno3 Nome Pai_Aluno3 \n", | |
| "4 Aluno3 pop2@pop.com.br Escola 1 Nome Mae_Aluno3 Nome Pai_Aluno3 \n", | |
| "5 Aluno3 pop3@pop.com.br Escola 1 Nome Mae_Aluno3 Nome Pai_Aluno3 \n", | |
| "6 Aluno4 pop1@pop.com.br Escola 1 Nome Mae_Aluno4 Nome Pai_Aluno4 \n", | |
| "7 Aluno4 pop2@pop.com.br Escola 1 Nome Mae_Aluno4 Nome Pai_Aluno4 \n", | |
| "8 Aluno4 pop3@pop.com.br Escola 1 Nome Mae_Aluno4 Nome Pai_Aluno4 \n", | |
| "9 Aluno4 pop4@pop.com.br Escola 1 Nome Mae_Aluno4 Nome Pai_Aluno4 \n", | |
| "10 Aluno5 pop1@pop.com.br Escola 1 Nome Mae_Aluno5 Nome Pai_Aluno5 \n", | |
| "11 Aluno5 pop2@pop.com.br Escola 1 Nome Mae_Aluno5 Nome Pai_Aluno5 \n", | |
| "12 Aluno5 pop3@pop.com.br Escola 1 Nome Mae_Aluno5 Nome Pai_Aluno5 \n", | |
| "13 Aluno5 pop4@pop.com.br Escola 1 Nome Mae_Aluno5 Nome Pai_Aluno5 \n", | |
| "14 Aluno5 pop5@pop.com.br 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 | |
| } |
Para transformar Query do Django em DataFrame:
df = pd.DataFrame(list(User.objects.all().values()))
Pra mostrar somente algumas colunas:
df[['id', 'email']]
Transformando todos os campos do DataFrame num jSON
df.T.apply(dict).tolist()
Renomeando colunas
df.rename(columns={'old_name': 'new_name'})
Selecionando as colunas de 10 em 10
df.iloc[:, :10]
com transposição
df.T.iloc[:10]
Novo df com algumas colunas.
new = old.filter(['A','B','D'], axis=1)
Como não inserir chaves com valor nulo no dicionário:
df.T.apply(lambda x: dict(x.dropna())).tolist()
Retornando os valores cujo tamanho da string seja maior que...
df[df['foo'].str.len() > 50]['foo']
Mostrando o tamanho de cada célula
df[df['foo'].str.len() > 50]['foo'].str.len()
Mostrando o tamanho de cada célula da coluna
df['foo_len'] = df['foo'].apply(len)
df[['foo_len', 'foo']]
Ou
df['foo'].str.len()
Suponha que você tenha Cliente e Obra.
Pegando o ID do Cliente que está em Obra e trocando pelo nome do Cliente que está no outro DataFrame.
# JSON do Cliente com IDCliente e Cliente
dict_cliente = df_cliente[['IDCliente', 'Cliente']].T.apply(dict).tolist()
[
{'IDCliente': 288, 'Cliente': 'Cliente Um'},
{'IDCliente': 1, 'Cliente': 'Cliente Dois'},
{'IDCliente': 959, 'Cliente': 'Cliente Três'},
]
Montando o dicionário que será usado como busca de cada Cliente a partir do seu ID.
_dict_cliente = {}
for item in dict_cliente:
_dict_cliente[item['IDCliente']] = item['Cliente']
_dict_cliente
{
288: 'Cliente Um',
1: 'Cliente Dois',
959: 'Cliente Três',
}
A partir desse dicionário fazemos a busca no outro DataFrame.
for row in df.itertuples():
nome_cliente = _dict_cliente.get(row.IDCliente)
print(row.IDCliente, nome_cliente)
288 Cliente Um
1 Cliente Dois
959 Cliente Três
Retornando o valor máximo agrupado por ano.
df.groupby(['Ano'])['NumeroOrcamento'].max()
df.reset_index()
Verificando data vazia:
for row in df.itertuples():
if row.DataOrcamento is pd.NaT:
print('Vazio')
else:
print(row.DataOrcamento)
Definindo vários fillna diferentes por coluna:
values = {'last_name': '', 'occupation': '', 'age': 0}
df = df.fillna(value=values)
df.head()
Se tiver problema com liblzma, faça um downgrade do Pandas para pandas==0.24.2.
https://stackoverflow.com/a/57115325
Retorna o tamanho do maior objeto de cada coluna.
dict_sizes = {}
for col in df.columns:
try:
print(f'{col} max length: {df[col].map(len).max()}\n')
dict_sizes[col] = df[col].map(len).max()
except Exception as e:
raise e
dict_sizes
dtype example
df['estoque'] =df['estoque'].fillna(0).astype(int)
Pandas Dataframe df to Django
https://www.laurivan.com/save-pandas-dataframe-as-django-model/
Produto.objects.bulk_create(
Produto(**item) for item in df.to_dict('records')
)
Definindo os tipos das colunas com dtype
dict_types_annot = {
'produto': str,
'ncm': str,
'preco': float,
'estoque': 'Int64',
}
# Define os tipos das colunas
dff = df.astype(dict_types_annot, errors='ignore')
# Troca 'nan' por None e float por None.
dff = dff.replace({'nan': None, float('nan'): None})
dff.to_dict('records')
Produto.objects.bulk_create(
Produto(**item) for item in dff.to_dict('records')
)
Intersecção de dataframes
import pandas as pd
import numpy as np
import datetime
from random import randint
df1 = pd.DataFrame({
'letters': ['A', 'B', 'C', 'D', 'E', 'J', 'K', 'M'],
'B': np.random.randint(0, 10, 8),
})
df1
df2 = pd.DataFrame({
'letters': ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'U', 'Z'],
'B': np.random.randint(0, 10, 26),
})
df2
# Retorna o que tem de comum nos dois dataframes.
pd.merge(df1, df2, how='inner', on='fruits')
# Retorna o que tem de comum, considerando o df1.
pd.merge(df1, df2, how='left', on='fruits')
# Retorna o que tem de comum, considerando o df2.
pd.merge(df1, df2, how='right', on='fruits')
Código que substitui da célula [6] em diante do
separe_email.ipynb:Um detalhe é a falta de um índice único dos registros. Então, supondo o índice automático decorrente da importação do arquivo Excel como ID válido, esse ID é o usado no "join" (
merge).