From cb93f9220d9b2d2548617b40729e1a5b57d39cb8 Mon Sep 17 00:00:00 2001 From: Benjifilly Date: Tue, 25 Jul 2023 11:12:49 +0200 Subject: [PATCH] feat: Pandas - Apply custom styles on column --- ...Pandas_Apply_custom_styles_on_column.ipynb | 455 ++++++++++++++++-- 1 file changed, 424 insertions(+), 31 deletions(-) diff --git a/Pandas/Pandas_Apply_custom_styles_on_column.ipynb b/Pandas/Pandas_Apply_custom_styles_on_column.ipynb index a95cda933b..1ca8976e1f 100644 --- a/Pandas/Pandas_Apply_custom_styles_on_column.ipynb +++ b/Pandas/Pandas_Apply_custom_styles_on_column.ipynb @@ -52,7 +52,7 @@ "tags": [] }, "source": [ - "**Last update:** 2023-07-24 (Created: 2023-07-24)" + "**Last update:** 2023-07-25 (Created: 2023-07-24)" ] }, { @@ -74,7 +74,9 @@ "tags": [] }, "source": [ - "**References:**\n- [Pandas Documentation - Styling](https://pandas.pydata.org/pandas-docs/stable/user_guide/style.html)\n- [Pandas Documentation - Styling with functions](https://pandas.pydata.org/pandas-docs/stable/user_guide/style.html#Styling-with-functions)" + "**References:**\n", + "- [Pandas Documentation - Styling](https://pandas.pydata.org/pandas-docs/stable/user_guide/style.html)\n", + "- [Pandas Documentation - Styling with functions](https://pandas.pydata.org/pandas-docs/stable/user_guide/style.html#Styling-with-functions)" ] }, { @@ -101,14 +103,23 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 12, "id": "63fda2d7-d1d9-4fd3-a752-13d9214d9acc", "metadata": { + "execution": { + "iopub.execute_input": "2023-07-25T08:53:07.565066Z", + "iopub.status.busy": "2023-07-25T08:53:07.564831Z", + "iopub.status.idle": "2023-07-25T08:53:07.568745Z", + "shell.execute_reply": "2023-07-25T08:53:07.568054Z", + "shell.execute_reply.started": "2023-07-25T08:53:07.565043Z" + }, "papermill": {}, "tags": [] }, - "source": "import pandas as pd", - "outputs": [] + "outputs": [], + "source": [ + "import pandas as pd" + ] }, { "cell_type": "markdown", @@ -123,14 +134,31 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 13, "id": "87d45a29-4fc3-4c06-af32-3536d91f6c55", "metadata": { + "execution": { + "iopub.execute_input": "2023-07-25T08:53:08.175954Z", + "iopub.status.busy": "2023-07-25T08:53:08.175607Z", + "iopub.status.idle": "2023-07-25T08:53:08.180488Z", + "shell.execute_reply": "2023-07-25T08:53:08.179776Z", + "shell.execute_reply.started": "2023-07-25T08:53:08.175916Z" + }, "papermill": {}, "tags": [] }, - "source": "# Create a fake dataset\ndata = {\"Name\": [\"Tom\", \"nick\", \"krish\", \"jack\"], \"Age\": [20, 21, 19, 18]}", - "outputs": [] + "outputs": [], + "source": [ + "# Sample DataFrame\n", + "data = {\n", + " 'Name': ['Alice', 'Bob', 'Charlie', 'David', 'Eva'],\n", + " 'Score': [95, -80, 70, -60, 85],\n", + " 'Age': [25, 32, 18, 40, 28],\n", + " 'Sales': [1200, 980, 1500, 850, 1750]\n", + "}\n", + "\n", + "df = pd.DataFrame(data)" + ] }, { "cell_type": "markdown", @@ -151,30 +179,314 @@ "tags": [] }, "source": [ - "### Apply custom styles on column" + "### Apply custom styles on column\n", + "In this example, we use a custom style function highlight_negative that applies the CSS attribute color: red to any cell in the `Score` column that contains a negative value. This will change the text color of negative values to red, making them stand out in the DataFrame." ] }, { - "cell_type": "markdown", - "id": "507b2b97-26e9-4d10-a5b9-eecebdf40435", + "cell_type": "code", + "execution_count": 25, + "id": "3e05ee25-4089-44f4-9c8d-4ad1f151e397", "metadata": { - "papermill": {}, + "execution": { + "iopub.execute_input": "2023-07-25T09:09:36.072495Z", + "iopub.status.busy": "2023-07-25T09:09:36.072267Z", + "iopub.status.idle": "2023-07-25T09:09:36.081539Z", + "shell.execute_reply": "2023-07-25T09:09:36.080880Z", + "shell.execute_reply.started": "2023-07-25T09:09:36.072471Z" + }, "tags": [] }, + "outputs": [ + { + "data": { + "text/html": [ + "\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
 NameScoreAgeSales
0Alice95251200
1Bob-8032980
2Charlie70181500
3David-6040850
4Eva85281750
\n" + ], + "text/plain": [ + "" + ] + }, + "execution_count": 25, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ - "This function will apply custom styles on a column of a Pandas DataFrame." + "df_highlight_negative = df.copy() # Copying the dataframe df into another variable\n", + "\n", + "# Define a function that returns the CSS styles based on the values of the column\n", + "def highlight_negative(s):\n", + " return ['color: red' if value < 0 else '' for value in s]\n", + "\n", + "df_highlight_negative = df_highlight_negative.style.apply(highlight_negative, subset=['Score'])\n", + "df_highlight_negative" + ] + }, + { + "cell_type": "markdown", + "id": "0ed200d1-4ca5-437f-b5d6-4d226a229f26", + "metadata": {}, + "source": [ + "The `color_scale` function applies a color scale to the `Age` column of the DataFrame. It assigns blue color to smaller values and red color to larger values, with varying intensity based on the values' magnitudes." ] }, { "cell_type": "code", - "execution_count": null, - "id": "928371a0-185c-40a3-a0b2-d8277d73e385", + "execution_count": 21, + "id": "27c05a6a-c1e5-4b4f-a575-9b4e3e6ace39", "metadata": { - "papermill": {}, + "execution": { + "iopub.execute_input": "2023-07-25T09:05:50.609554Z", + "iopub.status.busy": "2023-07-25T09:05:50.608274Z", + "iopub.status.idle": "2023-07-25T09:05:50.622612Z", + "shell.execute_reply": "2023-07-25T09:05:50.622096Z", + "shell.execute_reply.started": "2023-07-25T09:05:50.609488Z" + }, "tags": [] }, - "source": "# Create a DataFrame\ndf = pd.DataFrame(data)\n\n\n# Define a function to apply custom styles\ndef highlight_col(x):\n r = \"background-color: red\"\n df1 = pd.DataFrame(\"\", index=x.index, columns=x.columns)\n df1.iloc[:, 0] = r\n return df1\n\n\n# Apply the function\ndf.style.apply(highlight_col, axis=None)", - "outputs": [] + "outputs": [ + { + "data": { + "text/html": [ + "\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
 NameScoreAgeSales
0Alice95251200
1Bob-8032980
2Charlie70181500
3David-6040850
4Eva85281750
\n" + ], + "text/plain": [ + "" + ] + }, + "execution_count": 21, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "df_color_scale = df.copy()\n", + "\n", + "def color_scale(s):\n", + " min_val = min(s)\n", + " max_val = max(s)\n", + " color_range = ['background-color: blue; opacity: 0.6',\n", + " 'background-color: rgb(255, 0, 0); opacity: 0.6']\n", + " normalized_values = (s - min_val) / (max_val - min_val)\n", + " color_mapped = pd.cut(normalized_values, bins=[-0.001, 0.5, 1], labels=color_range)\n", + " return color_mapped\n", + "\n", + "# Apply the custom styles to the 'Age' column\n", + "df_color_scale = df_color_scale.style.apply(color_scale, subset=['Age'])\n", + "\n", + "# Display the styled DataFrame\n", + "df_color_scale" + ] + }, + { + "cell_type": "markdown", + "id": "d0799092-b280-4ec8-a261-ab08572a7984", + "metadata": {}, + "source": [ + "### Highlight Max\n", + "This style function is applied to the 'Sales' column, highlighting the cell with the maximum value using a purple background." + ] + }, + { + "cell_type": "code", + "execution_count": 17, + "id": "f6cfa95f-8f85-40a3-83cd-18edeb21692b", + "metadata": { + "execution": { + "iopub.execute_input": "2023-07-25T08:54:55.114983Z", + "iopub.status.busy": "2023-07-25T08:54:55.114747Z", + "iopub.status.idle": "2023-07-25T08:54:55.123220Z", + "shell.execute_reply": "2023-07-25T08:54:55.122609Z", + "shell.execute_reply.started": "2023-07-25T08:54:55.114960Z" + } + }, + "outputs": [ + { + "data": { + "text/html": [ + "\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
 NameScoreAgeSales
0Alice95251200
1Bob-8032980
2Charlie70181500
3David-6040850
4Eva85281750
\n" + ], + "text/plain": [ + "" + ] + }, + "execution_count": 17, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "df_highlight_max = df.copy()\n", + "\n", + "def highlight_max(s):\n", + " is_max = s == s.max()\n", + " return ['background-color: purple' if v else '' for v in is_max]\n", + "\n", + "df_highlight_max = df_highlight_max.style.apply(highlight_max, subset=['Sales'])\n", + "df_highlight_max" + ] }, { "cell_type": "markdown", @@ -200,24 +512,105 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 24, "id": "ffd0378b-42a0-4f59-8375-d6427ebc9094", "metadata": { + "execution": { + "iopub.execute_input": "2023-07-25T09:06:59.767969Z", + "iopub.status.busy": "2023-07-25T09:06:59.767705Z", + "iopub.status.idle": "2023-07-25T09:06:59.782924Z", + "shell.execute_reply": "2023-07-25T09:06:59.782261Z", + "shell.execute_reply.started": "2023-07-25T09:06:59.767943Z" + }, "papermill": {}, "tags": [] }, - "source": "# Display the DataFrame\ndf", - "outputs": [] - }, - { - "cell_type": "markdown", - "id": "7dd3e762-582f-4a65-a49c-38ccc6dd125f", - "metadata": { - "papermill": {}, - "tags": [] - }, + "outputs": [ + { + "data": { + "text/html": [ + "\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
 NameScoreAgeSales
0Alice95251200
1Bob-8032980
2Charlie70181500
3David-6040850
4Eva85281750
\n" + ], + "text/plain": [ + "" + ] + }, + "execution_count": 24, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ - "The column `Name` is now highlighted in red." + "# Apply the custom styles to different columns\n", + "styled_df = df.style.apply(highlight_negative, subset=['Score']) \\\n", + " .apply(color_scale, subset=['Age']) \\\n", + " .apply(highlight_max, subset=['Sales']) # Put \"\\\" when you want to apply more style\n", + "\n", + "# Display the styled DataFrame\n", + "styled_df" ] }, { @@ -271,4 +664,4 @@ }, "nbformat": 4, "nbformat_minor": 5 -} \ No newline at end of file +}