Skip to content
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
147 changes: 147 additions & 0 deletions SINADEF/Bot-Tweet-covid.2.0.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": 11,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Requirement already satisfied: seaborn in c:\\users\\user\\appdata\\local\\programs\\python\\python38\\lib\\site-packages (0.11.1)\n",
"Requirement already satisfied: matplotlib>=2.2 in c:\\users\\user\\appdata\\local\\programs\\python\\python38\\lib\\site-packages (from seaborn) (3.2.1)\n",
"Requirement already satisfied: scipy>=1.0 in c:\\users\\user\\appdata\\local\\programs\\python\\python38\\lib\\site-packages (from seaborn) (1.4.1)\n",
"Requirement already satisfied: numpy>=1.15 in c:\\users\\user\\appdata\\local\\programs\\python\\python38\\lib\\site-packages (from seaborn) (1.19.5)\n",
"Requirement already satisfied: pandas>=0.23 in c:\\users\\user\\appdata\\local\\programs\\python\\python38\\lib\\site-packages (from seaborn) (1.2.0)\n",
"Requirement already satisfied: cycler>=0.10 in c:\\users\\user\\appdata\\local\\programs\\python\\python38\\lib\\site-packages (from matplotlib>=2.2->seaborn) (0.10.0)\n",
"Requirement already satisfied: pyparsing!=2.0.4,!=2.1.2,!=2.1.6,>=2.0.1 in c:\\users\\user\\appdata\\local\\programs\\python\\python38\\lib\\site-packages (from matplotlib>=2.2->seaborn) (2.4.7)\n",
"Requirement already satisfied: kiwisolver>=1.0.1 in c:\\users\\user\\appdata\\local\\programs\\python\\python38\\lib\\site-packages (from matplotlib>=2.2->seaborn) (1.2.0)\n",
"Requirement already satisfied: python-dateutil>=2.1 in c:\\users\\user\\appdata\\local\\programs\\python\\python38\\lib\\site-packages (from matplotlib>=2.2->seaborn) (2.8.1)\n",
"Requirement already satisfied: six in c:\\users\\user\\appdata\\local\\programs\\python\\python38\\lib\\site-packages (from cycler>=0.10->matplotlib>=2.2->seaborn) (1.15.0)\n",
"Requirement already satisfied: pytz>=2017.3 in c:\\users\\user\\appdata\\local\\programs\\python\\python38\\lib\\site-packages (from pandas>=0.23->seaborn) (2020.1)\n"
]
}
],
"source": [
"!pip install seaborn --upgrade\n",
"# Trabajarlos como semanas."
]
},
{
"cell_type": "code",
"execution_count": 100,
"metadata": {},
"outputs": [],
"source": [
"## Sinadef\n",
"import pandas as pd\n",
"import numpy as np\n",
"import datetime\n",
"import matplotlib.pyplot as plt\n",
"import seaborn as sns\n",
"import math\n",
"from datetime import timedelta\n",
"import os\n",
"%matplotlib inline\n",
"pd.set_option('display.max_columns', None)\n",
"import time"
]
},
{
"cell_type": "code",
"execution_count": 105,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Tomó 2.09 segundos procesar los datos. \n",
"\n"
]
}
],
"source": [
"t0 = time.time()\n",
"name_file = 'fallecidos_covid.csv'\n",
"df_c = pd.read_csv(name_file, sep=';' , encoding='latin-1')\n",
"df = df_c.copy()\n",
"bins = [-1, 0, 10, 20, 30, 40, 50, 60, 70, 80, 90, float(\"inf\")]\n",
"df['FECHA_FALLECIMIENTO'] = df['FECHA_FALLECIMIENTO'].apply(lambda x: datetime.datetime.strptime(str(x), '%Y%m%d').date())\n",
"# 0 -> 0 ,1 -> 1 - 10, 2 -> 11 - 20, 3 -> 21- 30, 4 -> 31 - 40\n",
"# 5 -> 41 - 50, 6 -> 51 - 60, 7 -> 61 - 70, 8 -> 71 - 80, 9 -> 81 - 90, 10 -> 91+\n",
"df['Edad_fallecimiento'] = pd.cut(df['EDAD_DECLARADA'], bins)\n",
"df['Edad_fallecimiento'] = df['Edad_fallecimiento'].cat.codes\n",
"\n",
"# AGRUPAR DATOS POR FECHAS:\n",
"# SEXO\n",
"# EDAD_fallecimiento (por grupos)\n",
"# DEPARTAMENTO\n",
"\n",
"def df_por_dia(df, columna):\n",
" df_counts = pd.pivot_table(df , index = ['FECHA_FALLECIMIENTO'], columns = columna, aggfunc = 'size')\n",
" df_counts = df_counts[:-1]\n",
" df_counts = df_counts.fillna(0)\n",
" return df_counts\n",
"\n",
"def df_por_semana(df, columna):\n",
" df_aux = df.copy()\n",
" df_aux.loc[: , ('SEMANA_DEL_AÑO')] = df_aux['FECHA_FALLECIMIENTO'].apply(lambda x: (x.isocalendar()[:2]))\n",
" df_return = pd.pivot_table(df_aux, index = ['SEMANA_DEL_AÑO'],columns = columna, aggfunc = 'size')\n",
" df_return = df_return.fillna(0)\n",
" return df_return\n",
"\n",
"lista = ['SEXO', 'Edad_fallecimiento', 'DEPARTAMENTO']\n",
"\n",
"directory = os.path.join(os.getcwd(), 'results')\n",
"if not os.path.exists(directory):\n",
" os.mkdir(directory)\n",
"\n",
"for columna in lista:\n",
" data_frame = df_por_dia(df, columna)\n",
" data_frame.to_csv(directory + '//Por_dia_' + columna + '.csv')\n",
" data_frame_semana = df_por_semana(df, columna)\n",
" data_frame_semana.to_csv(directory + '//Por_semana_' + columna + '.csv')\n",
" \n",
"t1 = time.time()\n",
"print('Tomó {} segundos procesar los datos. \\n'.format(round(t1 - t0, 2)))"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"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.8.3rc1"
}
},
"nbformat": 4,
"nbformat_minor": 4
}
Loading