{"cells":[{"cell_type":"markdown","metadata":{"_cell_guid":"52bfe2c4-40c3-1b64-41f4-a15373e153e2"},"source":"go go go"},{"cell_type":"code","execution_count":null,"metadata":{"_cell_guid":"afab1fd1-cc45-abf5-1278-27f564fc66de"},"outputs":[],"source":"# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load in \n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\nimport matplotlib.pyplot as plt\n\n# Input data files are available in the \"../input/\" directory.\n# For example, running this (by clicking run or pressing Shift+Enter) will list the files in the input directory\n\n# cliente_tabla.csv\n# producto_tabla.csv\n# sample_submission.csv\n# test.csv\n# town_state.csv\n# train.csv\n\n# Product Mapping ID\nproducto_tabla = pd.read_csv('../input/producto_tabla.csv')\nproducto_tabla['First'] = producto_tabla['NombreProducto'].apply(lambda x: x.split(' ')[0])\nproducto_dict = dict()\nfor (i,x) in enumerate(pd.Series(producto_tabla['First'].unique())):\n    producto_dict[x] = i\nproducto_tabla['MapFirst'] = producto_tabla['First'].apply(lambda x: producto_dict[x])\ntemp = producto_tabla[['Producto_ID', 'MapFirst']].values\nproducto_cluster = dict()\nfor i in range(temp.shape[0]):\n    producto_cluster[temp[i,0]] = temp[i,1]\n\n# Town State Mapping ID\ntown_state = pd.read_csv('../input/town_state.csv')\ntown_state['T_S'] = town_state['Town'] + ':' + town_state['State']\ntown_state_dict = dict()\ns_t = town_state['T_S'].unique()\nfor i in range(s_t.shape[0]):\n    ts = s_t[i].split(':')\n    town_state_dict[(ts[0], ts[1])] = i\ntemp = town_state[['Agencia_ID', 'Town', 'State']].values\ntown_state_cluster = dict()\nfor i in range(temp.shape[0]):\n    if town_state_cluster.get(temp[i,0], -1) == -1:\n        town_state_cluster[temp[i,0]] = town_state_dict[(temp[i,1], temp[i,2])]\n    else:\n        pass\n    \n# Cliente ID Map\ncliente_tabla = pd.read_csv('../input/cliente_tabla.csv')\ncliente_tabla['First'] = cliente_tabla['NombreCliente'].apply(lambda x: x.split(' ')[0])\ntemp = dict()\ntnp = cliente_tabla['First'].unique()\nfor i in range(tnp.shape[0]):\n    temp[tnp[i]] = i\ncliente_tabla['MapFirst'] = cliente_tabla['First'].apply(lambda x: temp[x])\ntemp = cliente_tabla[['Cliente_ID', 'MapFirst']].values\ncliente_cluster = dict()\nfor i in range(temp.shape[0]):\n    if cliente_cluster.get(temp[i,0], -1) == -1:\n        cliente_cluster[temp[i,0]] = temp[i,1]\n    else:\n        pass\n# Any results you write to the current directory are saved as output."},{"cell_type":"code","execution_count":null,"metadata":{"_cell_guid":"2a99c801-d9dd-30e0-9c35-41e36a0626df"},"outputs":[],"source":"print(len(producto_cluster), len(town_state_cluster), len(cliente_cluster))"},{"cell_type":"code","execution_count":null,"metadata":{"_cell_guid":"b9d0d57a-f8b7-65a4-73c4-434db46840b0"},"outputs":[],"source":"with open('../working/pc.csv', 'wt') as f:\n    f.write('\"Producto_ID\",\"PC\"\\n')\n    for key in producto_cluster:\n        print(key, producto_cluster[key])\n        f.write(str(key)+','+str(value)+'\\n')"},{"cell_type":"code","execution_count":null,"metadata":{"_cell_guid":"73aff1aa-95a7-6627-4161-da3f476181c1"},"outputs":[],"source":"pc.head()"},{"cell_type":"code","execution_count":null,"metadata":{"_cell_guid":"3e3a4e4e-2b54-1740-482c-f45604189db3"},"outputs":[],"source":"colnames()"}],"metadata":{"_change_revision":0,"_is_fork":false,"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":0}