Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save anujonthemove/8352ba8f4749c62458043450fbdaf59b to your computer and use it in GitHub Desktop.
Save anujonthemove/8352ba8f4749c62458043450fbdaf59b to your computer and use it in GitHub Desktop.
Highlights in Pandas
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"metadata": {},
"cell_type": "markdown",
"source": "https://stackoverflow.com/questions/41203959/conditionally-format-python-pandas-cell"
},
{
"metadata": {
"trusted": false
},
"id": "9fc8ba0c",
"cell_type": "code",
"source": "import numpy as np\nimport pandas as pd",
"execution_count": 1,
"outputs": []
},
{
"metadata": {
"trusted": false
},
"id": "8b6e1a9a",
"cell_type": "code",
"source": "df = pd.DataFrame(np.random.rand(4,3))\ndf.style.applymap(lambda x: 'background-color : yellow' if x>df.iloc[0,0] else '')",
"execution_count": 2,
"outputs": [
{
"data": {
"text/html": "<style type=\"text/css\" >\n#T_422815f2_afa0_11eb_9cdc_a4c3f0a9e9a8row0_col1,#T_422815f2_afa0_11eb_9cdc_a4c3f0a9e9a8row0_col2,#T_422815f2_afa0_11eb_9cdc_a4c3f0a9e9a8row1_col0,#T_422815f2_afa0_11eb_9cdc_a4c3f0a9e9a8row1_col1,#T_422815f2_afa0_11eb_9cdc_a4c3f0a9e9a8row1_col2,#T_422815f2_afa0_11eb_9cdc_a4c3f0a9e9a8row2_col0,#T_422815f2_afa0_11eb_9cdc_a4c3f0a9e9a8row2_col1,#T_422815f2_afa0_11eb_9cdc_a4c3f0a9e9a8row2_col2,#T_422815f2_afa0_11eb_9cdc_a4c3f0a9e9a8row3_col0,#T_422815f2_afa0_11eb_9cdc_a4c3f0a9e9a8row3_col1,#T_422815f2_afa0_11eb_9cdc_a4c3f0a9e9a8row3_col2{\n background-color : yellow;\n }</style><table id=\"T_422815f2_afa0_11eb_9cdc_a4c3f0a9e9a8\" ><thead> <tr> <th class=\"blank level0\" ></th> <th class=\"col_heading level0 col0\" >0</th> <th class=\"col_heading level0 col1\" >1</th> <th class=\"col_heading level0 col2\" >2</th> </tr></thead><tbody>\n <tr>\n <th id=\"T_422815f2_afa0_11eb_9cdc_a4c3f0a9e9a8level0_row0\" class=\"row_heading level0 row0\" >0</th>\n <td id=\"T_422815f2_afa0_11eb_9cdc_a4c3f0a9e9a8row0_col0\" class=\"data row0 col0\" >0.080455</td>\n <td id=\"T_422815f2_afa0_11eb_9cdc_a4c3f0a9e9a8row0_col1\" class=\"data row0 col1\" >0.378008</td>\n <td id=\"T_422815f2_afa0_11eb_9cdc_a4c3f0a9e9a8row0_col2\" class=\"data row0 col2\" >0.861308</td>\n </tr>\n <tr>\n <th id=\"T_422815f2_afa0_11eb_9cdc_a4c3f0a9e9a8level0_row1\" class=\"row_heading level0 row1\" >1</th>\n <td id=\"T_422815f2_afa0_11eb_9cdc_a4c3f0a9e9a8row1_col0\" class=\"data row1 col0\" >0.571776</td>\n <td id=\"T_422815f2_afa0_11eb_9cdc_a4c3f0a9e9a8row1_col1\" class=\"data row1 col1\" >0.195861</td>\n <td id=\"T_422815f2_afa0_11eb_9cdc_a4c3f0a9e9a8row1_col2\" class=\"data row1 col2\" >0.586259</td>\n </tr>\n <tr>\n <th id=\"T_422815f2_afa0_11eb_9cdc_a4c3f0a9e9a8level0_row2\" class=\"row_heading level0 row2\" >2</th>\n <td id=\"T_422815f2_afa0_11eb_9cdc_a4c3f0a9e9a8row2_col0\" class=\"data row2 col0\" >0.664640</td>\n <td id=\"T_422815f2_afa0_11eb_9cdc_a4c3f0a9e9a8row2_col1\" class=\"data row2 col1\" >0.767051</td>\n <td id=\"T_422815f2_afa0_11eb_9cdc_a4c3f0a9e9a8row2_col2\" class=\"data row2 col2\" >0.291405</td>\n </tr>\n <tr>\n <th id=\"T_422815f2_afa0_11eb_9cdc_a4c3f0a9e9a8level0_row3\" class=\"row_heading level0 row3\" >3</th>\n <td id=\"T_422815f2_afa0_11eb_9cdc_a4c3f0a9e9a8row3_col0\" class=\"data row3 col0\" >0.831663</td>\n <td id=\"T_422815f2_afa0_11eb_9cdc_a4c3f0a9e9a8row3_col1\" class=\"data row3 col1\" >0.289736</td>\n <td id=\"T_422815f2_afa0_11eb_9cdc_a4c3f0a9e9a8row3_col2\" class=\"data row3 col2\" >0.856822</td>\n </tr>\n </tbody></table>",
"text/plain": "<pandas.io.formats.style.Styler at 0x7f816f0d3ac8>"
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
]
},
{
"metadata": {},
"cell_type": "markdown",
"source": "https://towardsdatascience.com/10-python-pandas-tricks-to-make-data-analysis-more-enjoyable-cb8f55af8c30"
},
{
"metadata": {
"trusted": false
},
"id": "f191d8c7",
"cell_type": "code",
"source": "def color_negative_red(val):\n color = 'red' if val < 0 else 'black'\n return 'color: %s' % color",
"execution_count": 5,
"outputs": []
},
{
"metadata": {
"trusted": false
},
"id": "cf3792da",
"cell_type": "code",
"source": "df = pd.DataFrame(dict(col_1=[1.53,-2.5,3.53], col_2=[-4.1,5.9,0]))\ndf.style.applymap(color_negative_red)",
"execution_count": 6,
"outputs": [
{
"data": {
"text/html": "<style type=\"text/css\" >\n#T_606d8c0e_afa0_11eb_9cdc_a4c3f0a9e9a8row0_col0,#T_606d8c0e_afa0_11eb_9cdc_a4c3f0a9e9a8row1_col1,#T_606d8c0e_afa0_11eb_9cdc_a4c3f0a9e9a8row2_col0,#T_606d8c0e_afa0_11eb_9cdc_a4c3f0a9e9a8row2_col1{\n color: black;\n }#T_606d8c0e_afa0_11eb_9cdc_a4c3f0a9e9a8row0_col1,#T_606d8c0e_afa0_11eb_9cdc_a4c3f0a9e9a8row1_col0{\n color: red;\n }</style><table id=\"T_606d8c0e_afa0_11eb_9cdc_a4c3f0a9e9a8\" ><thead> <tr> <th class=\"blank level0\" ></th> <th class=\"col_heading level0 col0\" >col_1</th> <th class=\"col_heading level0 col1\" >col_2</th> </tr></thead><tbody>\n <tr>\n <th id=\"T_606d8c0e_afa0_11eb_9cdc_a4c3f0a9e9a8level0_row0\" class=\"row_heading level0 row0\" >0</th>\n <td id=\"T_606d8c0e_afa0_11eb_9cdc_a4c3f0a9e9a8row0_col0\" class=\"data row0 col0\" >1.530000</td>\n <td id=\"T_606d8c0e_afa0_11eb_9cdc_a4c3f0a9e9a8row0_col1\" class=\"data row0 col1\" >-4.100000</td>\n </tr>\n <tr>\n <th id=\"T_606d8c0e_afa0_11eb_9cdc_a4c3f0a9e9a8level0_row1\" class=\"row_heading level0 row1\" >1</th>\n <td id=\"T_606d8c0e_afa0_11eb_9cdc_a4c3f0a9e9a8row1_col0\" class=\"data row1 col0\" >-2.500000</td>\n <td id=\"T_606d8c0e_afa0_11eb_9cdc_a4c3f0a9e9a8row1_col1\" class=\"data row1 col1\" >5.900000</td>\n </tr>\n <tr>\n <th id=\"T_606d8c0e_afa0_11eb_9cdc_a4c3f0a9e9a8level0_row2\" class=\"row_heading level0 row2\" >2</th>\n <td id=\"T_606d8c0e_afa0_11eb_9cdc_a4c3f0a9e9a8row2_col0\" class=\"data row2 col0\" >3.530000</td>\n <td id=\"T_606d8c0e_afa0_11eb_9cdc_a4c3f0a9e9a8row2_col1\" class=\"data row2 col1\" >0.000000</td>\n </tr>\n </tbody></table>",
"text/plain": "<pandas.io.formats.style.Styler at 0x7f816bcf5710>"
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
]
},
{
"metadata": {
"trusted": false
},
"id": "7b422f3c",
"cell_type": "code",
"source": "",
"execution_count": null,
"outputs": []
}
],
"metadata": {
"kernelspec": {
"name": "python3",
"display_name": "Python 3",
"language": "python"
},
"language_info": {
"name": "python",
"version": "3.6.9",
"mimetype": "text/x-python",
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"pygments_lexer": "ipython3",
"nbconvert_exporter": "python",
"file_extension": ".py"
},
"varInspector": {
"window_display": false,
"cols": {
"lenName": 16,
"lenType": 16,
"lenVar": 40
},
"kernels_config": {
"python": {
"library": "var_list.py",
"delete_cmd_prefix": "del ",
"delete_cmd_postfix": "",
"varRefreshCmd": "print(var_dic_list())"
},
"r": {
"library": "var_list.r",
"delete_cmd_prefix": "rm(",
"delete_cmd_postfix": ") ",
"varRefreshCmd": "cat(var_dic_list()) "
}
},
"types_to_exclude": [
"module",
"function",
"builtin_function_or_method",
"instance",
"_Feature"
]
},
"gist": {
"id": "",
"data": {
"description": "Highlights in Pandas",
"public": true
}
}
},
"nbformat": 4,
"nbformat_minor": 5
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment