{"cells":[{"metadata":{"_uuid":"685d92260a8d6d86b31ad886fe0891c3d2fdc332"},"cell_type":"markdown","source":"### Introduction\nThe data in this project is pretty disorganized. This kernel flattens out the jsons into and removes some columns that don't provide any data"},{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"cell_type":"code","source":"import numpy as np \nimport pandas as pd\nimport json\nfrom pandas.io.json import json_normalize\nimport os\nimport gc\nprint(os.listdir(\"../input\"))","execution_count":null,"outputs":[]},{"metadata":{"_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","trusted":true},"cell_type":"code","source":"def load_data(train_url, test_url):\n    \"\"\"\n    load train and test data \n    \"\"\"\n    dtype={\n            'channelGrouping': str,\n            'geoNetwork': str,\n            'date': str,\n            'fullVisitorId': str,\n            'sessionId': str,\n            'totals': str,\n            'device': str\n        }\n    \n    df_train = pd.read_csv(train_url, dtype=dtype)\n    df_test =  pd.read_csv(test_url, dtype=dtype)\n\n    df_train['is_train'] = 1\n    df_test['is_train'] = 0\n\n    df = pd.concat([df_train, df_test]).reset_index(drop=True)\n    del df_train; del df_test; gc.collect()\n    return df\n\ndef normalize_json_cols(df_t, json_cols):\n    \"\"\"\n    returns df with columns for keys in a column that contains a json string\n    \"\"\"\n    df = df_t.copy()\n    for i in json_cols:\n        temp = pd.io.json.json_normalize(df[i].apply(json.loads))\n        temp.columns = [i + '_' + j for j in temp.columns]\n        df = pd.concat([df, temp], axis=1)\n    df.drop(json_cols, axis=1, inplace=True)\n    return df\n\ndef remove_junk_data(df, dont_del=[]):\n    \"\"\"\n    remove columns with only 1 unique value\n    \"\"\"\n    unique_counts = df.nunique()\n    unique_counts.drop(dont_del, inplace=True)\n    return df.drop(list(unique_counts.index[unique_counts == 1]), axis=1)\n\ndef coerce_numeric_columns(df, numerics):\n    \"\"\"\n    force certain numeric columns to be... numeric\n    \"\"\"\n    df[numerics] = df[numerics].apply(pd.to_numeric, errors='coerce')\n    return df\n\ndef run():\n    \"\"\"\n    return the cleaned up dataframe\n    \"\"\"\n    df = load_data('../input/train.csv', '../input/test.csv')\n    json_cols = ['device', 'geoNetwork', 'totals', 'trafficSource']\n    dont_del = ['totals_newVisits', 'totals_bounces']\n    df = normalize_json_cols(df, json_cols)\n    df = remove_junk_data(df, dont_del=dont_del)\n    numerics = ['totals_hits', 'totals_pageviews', 'totals_transactionRevenue', 'totals_newVisits', 'totals_bounces']\n    df = coerce_numeric_columns(df, numerics)\n    return df","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"00284c51e9d5a9303cff3df1df3d619c0677bd02"},"cell_type":"code","source":"df = run()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"310a9d9d1b29b7fbaea2fe179c9e1f0aa9a9cac3"},"cell_type":"code","source":"df.to_csv('starting_point.csv', index=False)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"fab9bbf55ed4bc6bfffd0b5f2a147f92e68b5c56"},"cell_type":"code","source":"","execution_count":null,"outputs":[]}],"metadata":{"kernelspec":{"display_name":"Python 3","language":"python","name":"python3"},"language_info":{"name":"python","version":"3.6.6","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"}},"nbformat":4,"nbformat_minor":1}