{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"The purpose of this notebook is to convert csv file into parquet file. The benefit of parquet file is\n- have smaller file size compared to (uncompressed) csv file\n- can save and load dtypes\n- faster in loading\n\nThe precision level of float that I used is 'float32'. If the data range is not too big, you can change it into 'float16' in order to use less memory and loose some precisions under decimal point (which might be useless to keep).","metadata":{}},{"cell_type":"code","source":"import gc\nimport numpy as np\nimport pandas as pd","metadata":{"execution":{"iopub.status.busy":"2023-02-17T20:17:38.463122Z","iopub.execute_input":"2023-02-17T20:17:38.463776Z","iopub.status.idle":"2023-02-17T20:17:38.468743Z","shell.execute_reply.started":"2023-02-17T20:17:38.463740Z","shell.execute_reply":"2023-02-17T20:17:38.467726Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","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\n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n\n# Input data files are available in the read-only \"../input/\" directory\n# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n\nimport os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))\n\n# You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using \"Save & Run All\" \n# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2023-02-17T20:17:38.470347Z","iopub.execute_input":"2023-02-17T20:17:38.471047Z","iopub.status.idle":"2023-02-17T20:17:38.485475Z","shell.execute_reply.started":"2023-02-17T20:17:38.471012Z","shell.execute_reply":"2023-02-17T20:17:38.484319Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def get_df(which):\n    col_list = [\n        'session_id', 'index', 'elapsed_time', 'event_name', 'name', 'level', 'page',\n        'room_coor_x', 'room_coor_y', 'screen_coor_x', 'screen_coor_y', 'hover_duration',\n        'text', 'fqid', 'room_fqid', 'text_fqid', 'level_group'\n    ]\n    if which == 'train':\n        df_location = '/kaggle/input/predict-student-performance-from-game-play/train.csv'\n    elif which == 'test':\n        df_location = '/kaggle/input/predict-student-performance-from-game-play/test.csv'\n    else:\n        raise ValueError('Wrong df name.')\n    \n    df = pd.read_csv(df_location, usecols = col_list)\n    \n    for col in df.columns:\n        if 'int' in df.loc[:, col].dtypes.name:\n            df.loc[:, col] = pd.to_numeric(df.loc[:, col], downcast='integer')\n        elif 'float' in df.loc[:, col].dtypes.name:\n            df.loc[:, col] = pd.to_numeric(df.loc[:, col], downcast='float')\n        else:\n            df.loc[:, col] = df.loc[:, col].astype('category')\n    return df","metadata":{"execution":{"iopub.status.busy":"2023-02-17T20:18:17.718091Z","iopub.execute_input":"2023-02-17T20:18:17.718835Z","iopub.status.idle":"2023-02-17T20:18:17.731482Z","shell.execute_reply.started":"2023-02-17T20:18:17.718778Z","shell.execute_reply":"2023-02-17T20:18:17.729875Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df = get_df('train')\nprint(train_df.dtypes)\ntrain_df.to_parquet('train.parquet')\ndel train_df\n_ = gc.collect()","metadata":{"execution":{"iopub.status.busy":"2023-02-17T20:18:20.301346Z","iopub.execute_input":"2023-02-17T20:18:20.301871Z","iopub.status.idle":"2023-02-17T20:19:01.517965Z","shell.execute_reply.started":"2023-02-17T20:18:20.301834Z","shell.execute_reply":"2023-02-17T20:19:01.514099Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_df = get_df('test')\nprint(test_df.dtypes)\ntest_df.to_parquet('test.parquet')\ndel test_df\n_ = gc.collect()","metadata":{"execution":{"iopub.status.busy":"2023-02-17T20:19:01.521323Z","iopub.status.idle":"2023-02-17T20:19:01.521892Z","shell.execute_reply.started":"2023-02-17T20:19:01.521646Z","shell.execute_reply":"2023-02-17T20:19:01.521668Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_labels = pd.read_csv('/kaggle/input/predict-student-performance-from-game-play/train_labels.csv')\ntrain_labels.loc[:, 'session_id'] = train_labels.loc[:, 'session_id'].astype('category')\ntrain_labels.loc[:, 'correct'] = pd.to_numeric(train_labels.loc[:, 'correct'], downcast='integer')\ntrain_labels.to_parquet('train_labels.parquet')\ndel train_labels\n_ = gc.collect()","metadata":{"execution":{"iopub.status.busy":"2023-02-17T20:21:40.790353Z","iopub.execute_input":"2023-02-17T20:21:40.790860Z","iopub.status.idle":"2023-02-17T20:21:42.154605Z","shell.execute_reply.started":"2023-02-17T20:21:40.790824Z","shell.execute_reply":"2023-02-17T20:21:42.153536Z"},"trusted":true},"execution_count":null,"outputs":[]}]}