Styling Pandas DataFrame

Source 1

Source 2

Import library

In [1]:
import numpy as np
import pandas as pd

Download and select data

In [2]:
! wget https://raw.githubusercontent.com/dataprofessor/data/master/nba-player-stats-2019.csv
--2021-04-17 15:38:29--  https://raw.githubusercontent.com/dataprofessor/data/master/nba-player-stats-2019.csv
Resolving raw.githubusercontent.com (raw.githubusercontent.com)... 185.199.110.133, 185.199.111.133, 185.199.108.133, ...
Connecting to raw.githubusercontent.com (raw.githubusercontent.com)|185.199.110.133|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 90308 (88K) [text/plain]
Saving to: ‘nba-player-stats-2019.csv.1’

nba-player-stats-20 100%[===================>]  88.19K   334KB/s    in 0.3s    

2021-04-17 15:38:31 (334 KB/s) - ‘nba-player-stats-2019.csv.1’ saved [90308/90308]

In [3]:
df = pd.read_csv('nba-player-stats-2019.csv')

df.head()
Out[3]:
Player Pos Age Tm G GS MP FG FGA FG% ... FT% ORB DRB TRB AST STL BLK TOV PF PTS
0 Álex Abrines SG 25 OKC 31 2 19.0 1.8 5.1 0.357 ... 0.923 0.2 1.4 1.5 0.6 0.5 0.2 0.5 1.7 5.3
1 Quincy Acy PF 28 PHO 10 0 12.3 0.4 1.8 0.222 ... 0.700 0.3 2.2 2.5 0.8 0.1 0.4 0.4 2.4 1.7
2 Jaylen Adams PG 22 ATL 34 1 12.6 1.1 3.2 0.345 ... 0.778 0.3 1.4 1.8 1.9 0.4 0.1 0.8 1.3 3.2
3 Steven Adams C 25 OKC 80 80 33.4 6.0 10.1 0.595 ... 0.500 4.9 4.6 9.5 1.6 1.5 1.0 1.7 2.6 13.9
4 Bam Adebayo C 21 MIA 82 28 23.3 3.4 5.9 0.576 ... 0.735 2.0 5.3 7.3 2.2 0.9 0.8 1.5 2.5 8.9

5 rows × 29 columns

In [4]:
df = df[['Player','Pos','Age','Tm','G','3P%','2P%','FT%','AST','STL','BLK','PTS']]

df.head()
Out[4]:
Player Pos Age Tm G 3P% 2P% FT% AST STL BLK PTS
0 Álex Abrines SG 25 OKC 31 0.323 0.500 0.923 0.6 0.5 0.2 5.3
1 Quincy Acy PF 28 PHO 10 0.133 0.667 0.700 0.8 0.1 0.4 1.7
2 Jaylen Adams PG 22 ATL 34 0.338 0.361 0.778 1.9 0.4 0.1 3.2
3 Steven Adams C 25 OKC 80 0.000 0.596 0.500 1.6 1.5 1.0 13.9
4 Bam Adebayo C 21 MIA 82 0.200 0.588 0.735 2.2 0.9 0.8 8.9

Style Pandas Table with CSS

In [5]:
df.head(20).style.set_table_styles(
[{'selector': 'th',
  'props': [('background', '#7CCE00'), 
            ('color', 'white'),
            ('font-family', 'verdana')]},
 
 {'selector': 'td',
  'props': [('font-family', 'verdana')]},

 {'selector': 'tr:nth-of-type(odd)',
  'props': [('background', '#DCDCDC')]}, 

 {'selector': 'tr:nth-of-type(even)',
  'props': [('background', 'white')]},
 
]
).hide_index()
Out[5]:
Player Pos Age Tm G 3P% 2P% FT% AST STL BLK PTS
Álex Abrines SG 25 OKC 31 0.323000 0.500000 0.923000 0.600000 0.500000 0.200000 5.300000
Quincy Acy PF 28 PHO 10 0.133000 0.667000 0.700000 0.800000 0.100000 0.400000 1.700000
Jaylen Adams PG 22 ATL 34 0.338000 0.361000 0.778000 1.900000 0.400000 0.100000 3.200000
Steven Adams C 25 OKC 80 0.000000 0.596000 0.500000 1.600000 1.500000 1.000000 13.900000
Bam Adebayo C 21 MIA 82 0.200000 0.588000 0.735000 2.200000 0.900000 0.800000 8.900000
Deng Adel SF 21 CLE 19 0.261000 0.385000 1.000000 0.300000 0.100000 0.200000 1.700000
DeVaughn Akoon-Purcell SG 25 DEN 7 0.000000 0.500000 0.500000 0.900000 0.300000 0.000000 1.000000
LaMarcus Aldridge C 33 SAS 81 0.238000 0.528000 0.847000 2.400000 0.500000 1.300000 21.300000
Rawle Alkins SG 21 CHI 10 0.250000 0.370000 0.667000 1.300000 0.100000 0.000000 3.700000
Grayson Allen SG 23 UTA 38 0.323000 0.443000 0.750000 0.700000 0.200000 0.200000 5.600000
Jarrett Allen C 20 BRK 80 0.133000 0.629000 0.709000 1.400000 0.500000 1.500000 10.900000
Kadeem Allen SG 26 NYK 19 0.472000 0.457000 0.778000 4.000000 0.800000 0.200000 9.900000
Al-Farouq Aminu PF 28 POR 81 0.343000 0.514000 0.867000 1.300000 0.800000 0.400000 9.400000
Justin Anderson SF 25 ATL 48 0.312000 0.500000 0.743000 0.500000 0.500000 0.300000 3.700000
Kyle Anderson SF 25 MEM 43 0.265000 0.583000 0.578000 3.000000 1.300000 0.900000 8.000000
Ryan Anderson PF 30 TOT 25 0.225000 0.414000 0.750000 0.800000 0.200000 0.000000 2.500000
Ryan Anderson PF 30 PHO 15 0.206000 0.462000 0.786000 1.100000 0.200000 0.100000 3.700000
Ryan Anderson PF 30 MIA 10 0.333000 0.000000 0.500000 0.200000 0.100000 0.000000 0.700000
Ike Anigbogu C 20 IND 3 0.000000 0.000000 0.000000 0.300000 0.000000 0.300000 0.000000
Giannis Antetokounmpo PF 24 MIL 72 0.256000 0.641000 0.729000 5.900000 1.300000 1.500000 27.700000

Adding yellow highlight upon hover

In [6]:
df.head(20).style.set_table_styles(
[{'selector': 'th',
  'props': [('background', '#7CCE00'), 
            ('color', 'white'),
            ('font-family', 'verdana')]},
 
 {'selector': 'td',
  'props': [('font-family', 'verdana')]},

 {'selector': 'tr:nth-of-type(odd)',
  'props': [('background', '#DCDCDC')]}, 
 
 {'selector': 'tr:nth-of-type(even)',
  'props': [('background', 'white')]},
 
 {'selector': 'tr:hover',
  'props': [('background-color', 'yellow')]}

]
).hide_index()
Out[6]:
Player Pos Age Tm G 3P% 2P% FT% AST STL BLK PTS
Álex Abrines SG 25 OKC 31 0.323000 0.500000 0.923000 0.600000 0.500000 0.200000 5.300000
Quincy Acy PF 28 PHO 10 0.133000 0.667000 0.700000 0.800000 0.100000 0.400000 1.700000
Jaylen Adams PG 22 ATL 34 0.338000 0.361000 0.778000 1.900000 0.400000 0.100000 3.200000
Steven Adams C 25 OKC 80 0.000000 0.596000 0.500000 1.600000 1.500000 1.000000 13.900000
Bam Adebayo C 21 MIA 82 0.200000 0.588000 0.735000 2.200000 0.900000 0.800000 8.900000
Deng Adel SF 21 CLE 19 0.261000 0.385000 1.000000 0.300000 0.100000 0.200000 1.700000
DeVaughn Akoon-Purcell SG 25 DEN 7 0.000000 0.500000 0.500000 0.900000 0.300000 0.000000 1.000000
LaMarcus Aldridge C 33 SAS 81 0.238000 0.528000 0.847000 2.400000 0.500000 1.300000 21.300000
Rawle Alkins SG 21 CHI 10 0.250000 0.370000 0.667000 1.300000 0.100000 0.000000 3.700000
Grayson Allen SG 23 UTA 38 0.323000 0.443000 0.750000 0.700000 0.200000 0.200000 5.600000
Jarrett Allen C 20 BRK 80 0.133000 0.629000 0.709000 1.400000 0.500000 1.500000 10.900000
Kadeem Allen SG 26 NYK 19 0.472000 0.457000 0.778000 4.000000 0.800000 0.200000 9.900000
Al-Farouq Aminu PF 28 POR 81 0.343000 0.514000 0.867000 1.300000 0.800000 0.400000 9.400000
Justin Anderson SF 25 ATL 48 0.312000 0.500000 0.743000 0.500000 0.500000 0.300000 3.700000
Kyle Anderson SF 25 MEM 43 0.265000 0.583000 0.578000 3.000000 1.300000 0.900000 8.000000
Ryan Anderson PF 30 TOT 25 0.225000 0.414000 0.750000 0.800000 0.200000 0.000000 2.500000
Ryan Anderson PF 30 PHO 15 0.206000 0.462000 0.786000 1.100000 0.200000 0.100000 3.700000
Ryan Anderson PF 30 MIA 10 0.333000 0.000000 0.500000 0.200000 0.100000 0.000000 0.700000
Ike Anigbogu C 20 IND 3 0.000000 0.000000 0.000000 0.300000 0.000000 0.300000 0.000000
Giannis Antetokounmpo PF 24 MIL 72 0.256000 0.641000 0.729000 5.900000 1.300000 1.500000 27.700000

Conditional Coloring

In [7]:
def color_red(val):
  if val > 20:
    color = 'green'
  elif val > 5:
    color = 'red'
  else:
    color = 'black'
  return 'color: %s' % color


df[['PTS']].head(20).style.applymap(color_red)
Out[7]:
PTS
0 5.300000
1 1.700000
2 3.200000
3 13.900000
4 8.900000
5 1.700000
6 1.000000
7 21.300000
8 3.700000
9 5.600000
10 10.900000
11 9.900000
12 9.400000
13 3.700000
14 8.000000
15 2.500000
16 3.700000
17 0.700000
18 0.000000
19 27.700000

Further reading