{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.13","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"none","dataSources":[{"sourceId":50160,"databundleVersionId":7602123,"sourceType":"competition"}],"dockerImageVersionId":30646,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"# Styling dataframes with CSS\nI always forget how to style dataframes nicely in a Jupyter Notebook.\nSo, here's a short reminder.","metadata":{}},{"cell_type":"code","source":"import pandas as pd\nfrom pandas import option_context\nfrom pathlib import Path\nfrom IPython.display import display, HTML","metadata":{"_kg_hide-input":true,"_kg_hide-output":true,"execution":{"iopub.status.busy":"2024-02-11T01:43:14.607455Z","iopub.execute_input":"2024-02-11T01:43:14.607871Z","iopub.status.idle":"2024-02-11T01:43:14.613447Z","shell.execute_reply.started":"2024-02-11T01:43:14.607841Z","shell.execute_reply":"2024-02-11T01:43:14.612342Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"INPUT_PATH = Path('/kaggle/input/home-credit-credit-risk-model-stability')","metadata":{"execution":{"iopub.status.busy":"2024-02-11T01:30:47.921128Z","iopub.execute_input":"2024-02-11T01:30:47.921471Z","iopub.status.idle":"2024-02-11T01:30:47.925537Z","shell.execute_reply.started":"2024-02-11T01:30:47.921447Z","shell.execute_reply":"2024-02-11T01:30:47.924773Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# This is the default output\nfeature_definitions = pd.read_csv(\n    INPUT_PATH / \"feature_definitions.csv\"\n)\nfeature_definitions","metadata":{"execution":{"iopub.status.busy":"2024-02-11T01:34:15.797266Z","iopub.execute_input":"2024-02-11T01:34:15.797588Z","iopub.status.idle":"2024-02-11T01:34:15.809702Z","shell.execute_reply.started":"2024-02-11T01:34:15.797564Z","shell.execute_reply":"2024-02-11T01:34:15.808849Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Here we style just this particular output with an option_context \nwith option_context(\n    \"display.max_rows\",\n    None,\n    \"display.max_columns\",\n    None,\n    \"display.max_colwidth\",\n    None,\n):\n    styled_feature_definitions = feature_definitions.style.set_properties(\n        # align to top, see the effect in row 33\n        subset=['Variable'], **{'vertical-align': 'top'}\n    ).set_properties(\n        # Align left so that the text starts immediately\n        subset=['Description'], **{'text-align': 'left'}\n    )\n    display(styled_feature_definitions)","metadata":{"execution":{"iopub.status.busy":"2024-02-11T01:42:25.559642Z","iopub.execute_input":"2024-02-11T01:42:25.559981Z","iopub.status.idle":"2024-02-11T01:42:25.610192Z","shell.execute_reply.started":"2024-02-11T01:42:25.559956Z","shell.execute_reply":"2024-02-11T01:42:25.608954Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# back to default\nfeature_definitions","metadata":{"execution":{"iopub.status.busy":"2024-02-11T01:31:50.014245Z","iopub.execute_input":"2024-02-11T01:31:50.014588Z","iopub.status.idle":"2024-02-11T01:31:50.025103Z","shell.execute_reply.started":"2024-02-11T01:31:50.014561Z","shell.execute_reply":"2024-02-11T01:31:50.024041Z"},"trusted":true},"execution_count":null,"outputs":[]}]}