{"cells":[{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"cell_type":"code","source":"import os\nimport numpy as np\nimport pandas as pd\nimport json","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"ccfdf8f629a7f41f8d5ae8659f174bf9854cf2d9"},"cell_type":"code","source":"pd.set_option(\"display.max_rows\", None)\npd.set_option(\"display.max_columns\", None)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"2d7430042c1159664f990c4c542348706bd72b67"},"cell_type":"code","source":"#read train.csv\ndf = pd.read_csv(\"../input/train.csv\", index_col=3)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"dd12082f34783a389eee49cf09932b69bbdc80aa"},"cell_type":"code","source":"#specify json columns\njson_col = [\"device\", \"geoNetwork\", \"totals\", \"trafficSource\"]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"8a2082cc89a46d2a2d86d1f1b924a35d7a2a630b"},"cell_type":"code","source":"#JSON to columns \nfor col in json_col:\n    tmp_array = np.array(df[col])\n    \n    tmp_jsonstr = []\n    for i in range(len(tmp_array)):\n        tmp_jsonstr.append(json.loads(tmp_array[i]))\n    \n    tmp_df = pd.DataFrame(tmp_jsonstr, index=df.index)\n    df = pd.concat([df, tmp_df], axis=1)\n        \n#     #each col to each dataframe \n#     exec(\"df_json_{} = pd.DataFrame(tmp_jsonstr, index={})\".format(col, index))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"3b949b0c85dcf7d355720ac37ba960d32f266310"},"cell_type":"code","source":"#resolve nested\ndf_adwordsClickInfo = pd.DataFrame(list(df[\"adwordsClickInfo\"]), index=df.index)\ndf = pd.concat([df, df_adwordsClickInfo], axis=1)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"7d1746774a18c2e21e30054d66cbd7e0eb4a7f70"},"cell_type":"code","source":"df.head()","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"7fa549d6c19ca38485923b1a0b0d81d5277fe790"},"cell_type":"markdown","source":"-EOF"}],"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}