{ "cells": [ { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Original array:\n", " A B C D E\n", "0 1.0 1.329212 NaN -0.316280 -0.990810\n", "1 2.0 -1.070816 -1.438713 0.564417 0.295722\n", "2 3.0 -1.626404 0.219565 0.678805 1.889273\n", "3 4.0 0.961538 0.104011 NaN 0.850229\n", "4 5.0 NaN 1.057737 0.165562 0.515018\n", "5 6.0 -1.336936 0.562861 1.392855 -0.063328\n", "6 7.0 0.121668 1.207603 -0.002040 1.627796\n", "7 8.0 0.354493 1.037528 -0.385684 0.519818\n", "8 9.0 1.686583 -1.325963 1.428984 -2.089354\n", "9 10.0 -0.129820 0.631523 -0.586538 NaN\n", "\n", "Different background color:\n" ] }, { "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", " \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", "
A B C D E
011.32921nan-0.31628-0.99081
12-1.07082-1.438710.5644170.295722
23-1.62640.2195650.6788051.88927
340.9615380.104011nan0.850229
45nan1.057740.1655620.515018
56-1.336940.5628611.39285-0.063328
670.1216681.2076-0.002040211.6278
780.3544931.03753-0.3856840.519818
891.68658-1.325961.42898-2.08935
910-0.129820.631523-0.586538nan
" ], "text/plain": [ "" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import pandas as pd\n", "import numpy as np\n", "np.random.seed(24)\n", "df = pd.DataFrame({'A': np.linspace(1, 10, 10)})\n", "df = pd.concat([df, pd.DataFrame(np.random.randn(10, 4), columns=list('BCDE'))],\n", " axis=1)\n", "df.iloc[0, 2] = np.nan\n", "df.iloc[3, 3] = np.nan\n", "df.iloc[4, 1] = np.nan\n", "df.iloc[9, 4] = np.nan\n", "print(\"Original array:\")\n", "print(df)\n", "print(\"\\nDifferent background color:\")\n", "coldict = {'B':'red', 'D':'yellow'}\n", "\n", "def highlight_cols(x):\n", " #copy df to new - original data are not changed\n", " df = x.copy()\n", " #select all values to default value - red color\n", " df.loc[:,:] = 'background-color: red'\n", " #overwrite values grey color\n", " df[['B','C', 'E']] = 'background-color: grey'\n", " #return color df\n", " return df \n", "\n", "df.style.apply(highlight_cols, axis=None)" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.7.1" } }, "nbformat": 4, "nbformat_minor": 2 }