Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

S11 - Jheniffer #26

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
142 changes: 142 additions & 0 deletions exercicios/para-casa/atividade.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Introdução\n",
"Este conjunto de dados contém informações sobre gols marcados em partidas do Campeonato Brasileiro. Os campos incluem o ID da partida, a rodada em que a partida ocorreu, o nome do clube, o nome do atleta que marcou o gol e o minuto em que o gol foi marcado. Vamos explorar esses dados para entender melhor as estatísticas dos gols."
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [],
"source": [
"import pandas as pd\n",
"df_gols = pd.read_csv(\"campeonato-brasileiro-gols.csv\")"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"outputs": [
{
"ename": "PermissionError",
"evalue": "[Errno 13] Permission denied: 'campeonato-brasileiro-gols.csv'",
"output_type": "error",
"traceback": [
"\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[1;31mPermissionError\u001b[0m Traceback (most recent call last)",
"\u001b[1;32mc:\\Users\\jheni\\on26-python-s11-pandas-numpy-I\\exercicios\\para-casa\\atividade.ipynb Cell 3\u001b[0m line \u001b[0;36m1\n\u001b[1;32m----> <a href='vscode-notebook-cell:/c%3A/Users/jheni/on26-python-s11-pandas-numpy-I/exercicios/para-casa/atividade.ipynb#W4sZmlsZQ%3D%3D?line=0'>1</a>\u001b[0m df_gols\u001b[39m.\u001b[39;49mto_csv(\u001b[39m'\u001b[39;49m\u001b[39mcampeonato-brasileiro-gols.csv\u001b[39;49m\u001b[39m'\u001b[39;49m, index\u001b[39m=\u001b[39;49m\u001b[39mFalse\u001b[39;49;00m)\n",
"File \u001b[1;32m~\\AppData\\Local\\Packages\\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\\LocalCache\\local-packages\\Python310\\site-packages\\pandas\\core\\generic.py:3902\u001b[0m, in \u001b[0;36mNDFrame.to_csv\u001b[1;34m(self, path_or_buf, sep, na_rep, float_format, columns, header, index, index_label, mode, encoding, compression, quoting, quotechar, lineterminator, chunksize, date_format, doublequote, escapechar, decimal, errors, storage_options)\u001b[0m\n\u001b[0;32m 3891\u001b[0m df \u001b[39m=\u001b[39m \u001b[39mself\u001b[39m \u001b[39mif\u001b[39;00m \u001b[39misinstance\u001b[39m(\u001b[39mself\u001b[39m, ABCDataFrame) \u001b[39melse\u001b[39;00m \u001b[39mself\u001b[39m\u001b[39m.\u001b[39mto_frame()\n\u001b[0;32m 3893\u001b[0m formatter \u001b[39m=\u001b[39m DataFrameFormatter(\n\u001b[0;32m 3894\u001b[0m frame\u001b[39m=\u001b[39mdf,\n\u001b[0;32m 3895\u001b[0m header\u001b[39m=\u001b[39mheader,\n\u001b[1;32m (...)\u001b[0m\n\u001b[0;32m 3899\u001b[0m decimal\u001b[39m=\u001b[39mdecimal,\n\u001b[0;32m 3900\u001b[0m )\n\u001b[1;32m-> 3902\u001b[0m \u001b[39mreturn\u001b[39;00m DataFrameRenderer(formatter)\u001b[39m.\u001b[39;49mto_csv(\n\u001b[0;32m 3903\u001b[0m path_or_buf,\n\u001b[0;32m 3904\u001b[0m lineterminator\u001b[39m=\u001b[39;49mlineterminator,\n\u001b[0;32m 3905\u001b[0m sep\u001b[39m=\u001b[39;49msep,\n\u001b[0;32m 3906\u001b[0m encoding\u001b[39m=\u001b[39;49mencoding,\n\u001b[0;32m 3907\u001b[0m errors\u001b[39m=\u001b[39;49merrors,\n\u001b[0;32m 3908\u001b[0m compression\u001b[39m=\u001b[39;49mcompression,\n\u001b[0;32m 3909\u001b[0m quoting\u001b[39m=\u001b[39;49mquoting,\n\u001b[0;32m 3910\u001b[0m columns\u001b[39m=\u001b[39;49mcolumns,\n\u001b[0;32m 3911\u001b[0m index_label\u001b[39m=\u001b[39;49mindex_label,\n\u001b[0;32m 3912\u001b[0m mode\u001b[39m=\u001b[39;49mmode,\n\u001b[0;32m 3913\u001b[0m chunksize\u001b[39m=\u001b[39;49mchunksize,\n\u001b[0;32m 3914\u001b[0m quotechar\u001b[39m=\u001b[39;49mquotechar,\n\u001b[0;32m 3915\u001b[0m date_format\u001b[39m=\u001b[39;49mdate_format,\n\u001b[0;32m 3916\u001b[0m doublequote\u001b[39m=\u001b[39;49mdoublequote,\n\u001b[0;32m 3917\u001b[0m escapechar\u001b[39m=\u001b[39;49mescapechar,\n\u001b[0;32m 3918\u001b[0m storage_options\u001b[39m=\u001b[39;49mstorage_options,\n\u001b[0;32m 3919\u001b[0m )\n",
"File \u001b[1;32m~\\AppData\\Local\\Packages\\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\\LocalCache\\local-packages\\Python310\\site-packages\\pandas\\io\\formats\\format.py:1152\u001b[0m, in \u001b[0;36mDataFrameRenderer.to_csv\u001b[1;34m(self, path_or_buf, encoding, sep, columns, index_label, mode, compression, quoting, quotechar, lineterminator, chunksize, date_format, doublequote, escapechar, errors, storage_options)\u001b[0m\n\u001b[0;32m 1131\u001b[0m created_buffer \u001b[39m=\u001b[39m \u001b[39mFalse\u001b[39;00m\n\u001b[0;32m 1133\u001b[0m csv_formatter \u001b[39m=\u001b[39m CSVFormatter(\n\u001b[0;32m 1134\u001b[0m path_or_buf\u001b[39m=\u001b[39mpath_or_buf,\n\u001b[0;32m 1135\u001b[0m lineterminator\u001b[39m=\u001b[39mlineterminator,\n\u001b[1;32m (...)\u001b[0m\n\u001b[0;32m 1150\u001b[0m formatter\u001b[39m=\u001b[39m\u001b[39mself\u001b[39m\u001b[39m.\u001b[39mfmt,\n\u001b[0;32m 1151\u001b[0m )\n\u001b[1;32m-> 1152\u001b[0m csv_formatter\u001b[39m.\u001b[39;49msave()\n\u001b[0;32m 1154\u001b[0m \u001b[39mif\u001b[39;00m created_buffer:\n\u001b[0;32m 1155\u001b[0m \u001b[39massert\u001b[39;00m \u001b[39misinstance\u001b[39m(path_or_buf, StringIO)\n",
"File \u001b[1;32m~\\AppData\\Local\\Packages\\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\\LocalCache\\local-packages\\Python310\\site-packages\\pandas\\io\\formats\\csvs.py:247\u001b[0m, in \u001b[0;36mCSVFormatter.save\u001b[1;34m(self)\u001b[0m\n\u001b[0;32m 243\u001b[0m \u001b[39m\u001b[39m\u001b[39m\"\"\"\u001b[39;00m\n\u001b[0;32m 244\u001b[0m \u001b[39mCreate the writer & save.\u001b[39;00m\n\u001b[0;32m 245\u001b[0m \u001b[39m\"\"\"\u001b[39;00m\n\u001b[0;32m 246\u001b[0m \u001b[39m# apply compression and byte/text conversion\u001b[39;00m\n\u001b[1;32m--> 247\u001b[0m \u001b[39mwith\u001b[39;00m get_handle(\n\u001b[0;32m 248\u001b[0m \u001b[39mself\u001b[39;49m\u001b[39m.\u001b[39;49mfilepath_or_buffer,\n\u001b[0;32m 249\u001b[0m \u001b[39mself\u001b[39;49m\u001b[39m.\u001b[39;49mmode,\n\u001b[0;32m 250\u001b[0m encoding\u001b[39m=\u001b[39;49m\u001b[39mself\u001b[39;49m\u001b[39m.\u001b[39;49mencoding,\n\u001b[0;32m 251\u001b[0m errors\u001b[39m=\u001b[39;49m\u001b[39mself\u001b[39;49m\u001b[39m.\u001b[39;49merrors,\n\u001b[0;32m 252\u001b[0m compression\u001b[39m=\u001b[39;49m\u001b[39mself\u001b[39;49m\u001b[39m.\u001b[39;49mcompression,\n\u001b[0;32m 253\u001b[0m storage_options\u001b[39m=\u001b[39;49m\u001b[39mself\u001b[39;49m\u001b[39m.\u001b[39;49mstorage_options,\n\u001b[0;32m 254\u001b[0m ) \u001b[39mas\u001b[39;00m handles:\n\u001b[0;32m 255\u001b[0m \u001b[39m# Note: self.encoding is irrelevant here\u001b[39;00m\n\u001b[0;32m 256\u001b[0m \u001b[39mself\u001b[39m\u001b[39m.\u001b[39mwriter \u001b[39m=\u001b[39m csvlib\u001b[39m.\u001b[39mwriter(\n\u001b[0;32m 257\u001b[0m handles\u001b[39m.\u001b[39mhandle,\n\u001b[0;32m 258\u001b[0m lineterminator\u001b[39m=\u001b[39m\u001b[39mself\u001b[39m\u001b[39m.\u001b[39mlineterminator,\n\u001b[1;32m (...)\u001b[0m\n\u001b[0;32m 263\u001b[0m quotechar\u001b[39m=\u001b[39m\u001b[39mself\u001b[39m\u001b[39m.\u001b[39mquotechar,\n\u001b[0;32m 264\u001b[0m )\n\u001b[0;32m 266\u001b[0m \u001b[39mself\u001b[39m\u001b[39m.\u001b[39m_save()\n",
"File \u001b[1;32m~\\AppData\\Local\\Packages\\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\\LocalCache\\local-packages\\Python310\\site-packages\\pandas\\io\\common.py:863\u001b[0m, in \u001b[0;36mget_handle\u001b[1;34m(path_or_buf, mode, encoding, compression, memory_map, is_text, errors, storage_options)\u001b[0m\n\u001b[0;32m 858\u001b[0m \u001b[39melif\u001b[39;00m \u001b[39misinstance\u001b[39m(handle, \u001b[39mstr\u001b[39m):\n\u001b[0;32m 859\u001b[0m \u001b[39m# Check whether the filename is to be opened in binary mode.\u001b[39;00m\n\u001b[0;32m 860\u001b[0m \u001b[39m# Binary mode does not support 'encoding' and 'newline'.\u001b[39;00m\n\u001b[0;32m 861\u001b[0m \u001b[39mif\u001b[39;00m ioargs\u001b[39m.\u001b[39mencoding \u001b[39mand\u001b[39;00m \u001b[39m\"\u001b[39m\u001b[39mb\u001b[39m\u001b[39m\"\u001b[39m \u001b[39mnot\u001b[39;00m \u001b[39min\u001b[39;00m ioargs\u001b[39m.\u001b[39mmode:\n\u001b[0;32m 862\u001b[0m \u001b[39m# Encoding\u001b[39;00m\n\u001b[1;32m--> 863\u001b[0m handle \u001b[39m=\u001b[39m \u001b[39mopen\u001b[39;49m(\n\u001b[0;32m 864\u001b[0m handle,\n\u001b[0;32m 865\u001b[0m ioargs\u001b[39m.\u001b[39;49mmode,\n\u001b[0;32m 866\u001b[0m encoding\u001b[39m=\u001b[39;49mioargs\u001b[39m.\u001b[39;49mencoding,\n\u001b[0;32m 867\u001b[0m errors\u001b[39m=\u001b[39;49merrors,\n\u001b[0;32m 868\u001b[0m newline\u001b[39m=\u001b[39;49m\u001b[39m\"\u001b[39;49m\u001b[39m\"\u001b[39;49m,\n\u001b[0;32m 869\u001b[0m )\n\u001b[0;32m 870\u001b[0m \u001b[39melse\u001b[39;00m:\n\u001b[0;32m 871\u001b[0m \u001b[39m# Binary mode\u001b[39;00m\n\u001b[0;32m 872\u001b[0m handle \u001b[39m=\u001b[39m \u001b[39mopen\u001b[39m(handle, ioargs\u001b[39m.\u001b[39mmode)\n",
"\u001b[1;31mPermissionError\u001b[0m: [Errno 13] Permission denied: 'campeonato-brasileiro-gols.csv'"
]
}
],
"source": [
"df_gols.to_csv('campeonato-brasileiro-gols.csv', index=False)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Conclusão\n",
"As estatísticas básicas fornecem informações sobre a distribuição dos gols, como a contagem, a média dos minutos em que os gols foram marcados, o desvio padrão, etc."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"print(df_gols.head(3))"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"def preprocess_data(dataframe):\n",
" # Deleção de colunas\n",
" columns_to_drop = [\"clube\", \"atleta\"] \n",
" dataframe = dataframe.drop(columns=columns_to_drop)\n",
"\n",
" # Sort de valores\n",
" dataframe = dataframe.sort_values(by=\"minuto\")\n",
"\n",
" # Remoção de nulos\n",
" dataframe = dataframe.dropna(\"tipo_de_gol\")\n",
"\n",
" # Reset de índice\n",
" dataframe = dataframe.reset_index(drop=True)\n",
"\n",
" # Lista de colunas\n",
" columns_list = dataframe.columns.tolist(\"partida_id\")\n",
"\n",
" # Uma cópia do dataframe inicial\n",
" df_copy = dataframe.copy()\n",
"\n",
" return dataframe, df_copy, columns_list\n",
"\n",
"# Aplicando a função de pré-processamento\n",
"df_gols, df_copy, columns_list = preprocess_data(df_gols)\n",
"\n",
"print(df_gols.head())\n",
"print(columns_list)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"df_gols.to_csv('df_gols_preprocessado.csv', index=False)\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"mean_gols_por_rodada = df_gols['Gols'].groupby(df_gols['Rodada']).mean()\n",
"top_goleador = df_gols['Atleta'].value_counts().idxmax()\n",
"\n",
"print(f\"\\nMédia de gols por rodada:\\n{mean_gols_por_rodada}\")\n",
"print(f\"\\nJogador que marcou mais gols:\\n{top_goleador}\")"
]
}
],
"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.10.11"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Loading