PANDAS VISUALISATION
Padhai - Read Json, Visualise
Padhai_data_tabulation WITH COVID19 DATASET

Data tabulation from json files

In [1]:
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
In [2]:
url = 'https://api.covid19india.org/states_daily.json'
In [3]:
import urllib.request
In [4]:
urllib.request.urlretrieve(url, 'data.json')
Out[4]:
('data.json', <http.client.HTTPMessage at 0x7f5032a97710>)
In [5]:
df = pd.read_json('data.json')
In [ ]:
df
Out[ ]:
states_daily
0 {'an': '0', 'ap': '1', 'ar': '0', 'as': '0', '...
1 {'an': '0', 'ap': '0', 'ar': '0', 'as': '0', '...
2 {'an': '0', 'ap': '0', 'ar': '0', 'as': '0', '...
3 {'an': '0', 'ap': '0', 'ar': '0', 'as': '0', '...
4 {'an': '0', 'ap': '0', 'ar': '0', 'as': '0', '...
... ...
1558 {'an': '2', 'ap': '1835', 'ar': '255', 'as': '...
1559 {'an': '0', 'ap': '16', 'ar': '0', 'as': '10',...
1560 {'an': '1', 'ap': '909', 'ar': '165', 'as': '7...
1561 {'an': '0', 'ap': '1543', 'ar': '249', 'as': '...
1562 {'an': '0', 'ap': '13', 'ar': '0', 'as': '10',...

1563 rows × 1 columns

the above format is not what is required, but in tabular format, hence

  • load it into dictionary, and then
  • load it into pandas DataFrame
In [120]:
import json
In [ ]:
with open('data.json') as f:
    data = json.load(f)
data

# output is displayed in a very long list of values in json file format
In [125]:
data = data['states_daily']

noralise function is used to format data into resepective rows and cols

In [9]:
df = pd.json_normalize(data)
In [10]:
df
Out[10]:
an ap ar as br ch ct date dateymd dd ... sk status tg tn tr tt un up ut wb
0 0 1 0 0 0 0 0 14-Mar-20 2020-03-14 0 ... 0 Confirmed 1 1 0 81 0 12 0 0
1 0 0 0 0 0 0 0 14-Mar-20 2020-03-14 0 ... 0 Recovered 0 0 0 9 0 4 0 0
2 0 0 0 0 0 0 0 14-Mar-20 2020-03-14 0 ... 0 Deceased 0 0 0 2 0 0 0 0
3 0 0 0 0 0 0 0 15-Mar-20 2020-03-15 0 ... 0 Confirmed 2 0 0 27 0 1 0 0
4 0 0 0 0 0 0 0 15-Mar-20 2020-03-15 0 ... 0 Recovered 1 0 0 4 0 0 0 0
... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ...
1558 2 1835 255 857 38 1 114 15-Aug-21 2021-08-15 0 ... 213 Recovered 582 1842 253 35936 0 36 31 709
1559 0 16 0 10 0 0 1 15-Aug-21 2021-08-15 0 ... 0 Deceased 1 23 4 421 0 1 0 12
1560 1 909 165 758 14 2 68 16-Aug-21 2021-08-16 0 ... 20 Confirmed 405 1851 52 24696 0 17 18 502
1561 0 1543 249 1014 42 3 224 16-Aug-21 2021-08-16 0 ... 147 Recovered 577 1911 223 36871 0 36 54 691
1562 0 13 0 10 0 0 1 16-Aug-21 2021-08-16 0 ... 0 Deceased 3 28 1 438 0 1 1 9

1563 rows × 42 columns

now, the job is to format this into tabular form

  • with index as date, and status
  • column removed
In [11]:
df.date = pd.to_datetime(df.date)
In [ ]:
df
Out[ ]:
an ap ar as br ch ct date dateymd dd ... sk status tg tn tr tt un up ut wb
0 0 1 0 0 0 0 0 2020-03-14 2020-03-14 0 ... 0 Confirmed 1 1 0 81 0 12 0 0
1 0 0 0 0 0 0 0 2020-03-14 2020-03-14 0 ... 0 Recovered 0 0 0 9 0 4 0 0
2 0 0 0 0 0 0 0 2020-03-14 2020-03-14 0 ... 0 Deceased 0 0 0 2 0 0 0 0
3 0 0 0 0 0 0 0 2020-03-15 2020-03-15 0 ... 0 Confirmed 2 0 0 27 0 1 0 0
4 0 0 0 0 0 0 0 2020-03-15 2020-03-15 0 ... 0 Recovered 1 0 0 4 0 0 0 0
... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ...
1558 2 1835 255 857 38 1 114 2021-08-15 2021-08-15 0 ... 213 Recovered 582 1842 253 35936 0 36 31 709
1559 0 16 0 10 0 0 1 2021-08-15 2021-08-15 0 ... 0 Deceased 1 23 4 421 0 1 0 12
1560 1 909 165 758 14 2 68 2021-08-16 2021-08-16 0 ... 20 Confirmed 405 1851 52 24696 0 17 18 502
1561 0 1543 249 1014 42 3 224 2021-08-16 2021-08-16 0 ... 147 Recovered 577 1911 223 36871 0 36 54 691
1562 0 13 0 10 0 0 1 2021-08-16 2021-08-16 0 ... 0 Deceased 3 28 1 438 0 1 1 9

1563 rows × 42 columns

limit dataframe only to Confirmed records

In [12]:
df = df[df.status == "Confirmed"]
In [13]:
df
Out[13]:
an ap ar as br ch ct date dateymd dd ... sk status tg tn tr tt un up ut wb
0 0 1 0 0 0 0 0 2020-03-14 2020-03-14 0 ... 0 Confirmed 1 1 0 81 0 12 0 0
3 0 0 0 0 0 0 0 2020-03-15 2020-03-15 0 ... 0 Confirmed 2 0 0 27 0 1 0 0
6 0 0 0 0 0 0 0 2020-03-16 2020-03-16 0 ... 0 Confirmed 1 0 0 15 0 0 1 0
9 0 0 0 0 0 0 0 2020-03-17 2020-03-17 0 ... 0 Confirmed 1 0 0 11 0 2 0 1
12 0 0 0 0 0 0 0 2020-03-18 2020-03-18 0 ... 0 Confirmed 8 1 0 37 0 2 1 0
... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ...
1548 0 1859 180 935 43 12 98 2021-08-12 2021-08-12 0 ... 100 Confirmed 453 1942 140 40081 0 15 24 747
1551 0 1746 166 763 47 15 77 2021-08-13 2021-08-13 0 ... 150 Confirmed 427 1933 180 38761 0 25 27 739
1554 0 1535 161 755 39 4 83 2021-08-14 2021-08-14 0 ... 129 Confirmed 420 1916 137 36135 0 42 19 705
1557 0 1506 48 411 28 1 49 2021-08-15 2021-08-15 0 ... 152 Confirmed 245 1896 142 33245 0 30 16 673
1560 1 909 165 758 14 2 68 2021-08-16 2021-08-16 0 ... 20 Confirmed 405 1851 52 24696 0 17 18 502

521 rows × 42 columns

In [14]:
df.drop(['status'], axis = 1, inplace = True)
/usr/local/lib/python3.7/dist-packages/pandas/core/frame.py:4913: SettingWithCopyWarning: 
A value is trying to be set on a copy of a slice from a DataFrame

See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy
  errors=errors,
In [15]:
df.set_index('date', inplace = True)
In [16]:
df
Out[16]:
an ap ar as br ch ct dateymd dd dl ... rj sk tg tn tr tt un up ut wb
date
2020-03-14 0 1 0 0 0 0 0 2020-03-14 0 7 ... 3 0 1 1 0 81 0 12 0 0
2020-03-15 0 0 0 0 0 0 0 2020-03-15 0 0 ... 1 0 2 0 0 27 0 1 0 0
2020-03-16 0 0 0 0 0 0 0 2020-03-16 0 0 ... 0 0 1 0 0 15 0 0 1 0
2020-03-17 0 0 0 0 0 0 0 2020-03-17 0 1 ... 0 0 1 0 0 11 0 2 0 1
2020-03-18 0 0 0 0 0 0 0 2020-03-18 0 2 ... 3 0 8 1 0 37 0 2 1 0
... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ...
2021-08-12 0 1859 180 935 43 12 98 2021-08-12 0 49 ... 17 100 453 1942 140 40081 0 15 24 747
2021-08-13 0 1746 166 763 47 15 77 2021-08-13 0 50 ... 24 150 427 1933 180 38761 0 25 27 739
2021-08-14 0 1535 161 755 39 4 83 2021-08-14 0 50 ... 14 129 420 1916 137 36135 0 42 19 705
2021-08-15 0 1506 48 411 28 1 49 2021-08-15 0 53 ... 18 152 245 1896 142 33245 0 30 16 673
2021-08-16 1 909 165 758 14 2 68 2021-08-16 0 27 ... 11 20 405 1851 52 24696 0 17 18 502

521 rows × 40 columns

In [17]:
df.drop('dateymd', inplace = True, axis=1)
/usr/local/lib/python3.7/dist-packages/pandas/core/frame.py:4913: SettingWithCopyWarning: 
A value is trying to be set on a copy of a slice from a DataFrame

See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy
  errors=errors,
In [18]:
df
Out[18]:
an ap ar as br ch ct dd dl dn ... rj sk tg tn tr tt un up ut wb
date
2020-03-14 0 1 0 0 0 0 0 0 7 0 ... 3 0 1 1 0 81 0 12 0 0
2020-03-15 0 0 0 0 0 0 0 0 0 0 ... 1 0 2 0 0 27 0 1 0 0
2020-03-16 0 0 0 0 0 0 0 0 0 0 ... 0 0 1 0 0 15 0 0 1 0
2020-03-17 0 0 0 0 0 0 0 0 1 0 ... 0 0 1 0 0 11 0 2 0 1
2020-03-18 0 0 0 0 0 0 0 0 2 0 ... 3 0 8 1 0 37 0 2 1 0
... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ...
2021-08-12 0 1859 180 935 43 12 98 0 49 1 ... 17 100 453 1942 140 40081 0 15 24 747
2021-08-13 0 1746 166 763 47 15 77 0 50 0 ... 24 150 427 1933 180 38761 0 25 27 739
2021-08-14 0 1535 161 755 39 4 83 0 50 0 ... 14 129 420 1916 137 36135 0 42 19 705
2021-08-15 0 1506 48 411 28 1 49 0 53 0 ... 18 152 245 1896 142 33245 0 30 16 673
2021-08-16 1 909 165 758 14 2 68 0 27 2 ... 11 20 405 1851 52 24696 0 17 18 502

521 rows × 39 columns

because the data is from json, all the datatypes are in 'Object' instead of int64,

  • convert object datatype to int64
In [19]:
df.info()
<class 'pandas.core.frame.DataFrame'>
DatetimeIndex: 521 entries, 2020-03-14 to 2021-08-16
Data columns (total 39 columns):
 #   Column  Non-Null Count  Dtype 
---  ------  --------------  ----- 
 0   an      521 non-null    object
 1   ap      521 non-null    object
 2   ar      521 non-null    object
 3   as      521 non-null    object
 4   br      521 non-null    object
 5   ch      521 non-null    object
 6   ct      521 non-null    object
 7   dd      521 non-null    object
 8   dl      521 non-null    object
 9   dn      521 non-null    object
 10  ga      521 non-null    object
 11  gj      521 non-null    object
 12  hp      521 non-null    object
 13  hr      521 non-null    object
 14  jh      521 non-null    object
 15  jk      521 non-null    object
 16  ka      521 non-null    object
 17  kl      521 non-null    object
 18  la      521 non-null    object
 19  ld      521 non-null    object
 20  mh      521 non-null    object
 21  ml      521 non-null    object
 22  mn      521 non-null    object
 23  mp      521 non-null    object
 24  mz      521 non-null    object
 25  nl      521 non-null    object
 26  or      521 non-null    object
 27  pb      521 non-null    object
 28  py      521 non-null    object
 29  rj      521 non-null    object
 30  sk      521 non-null    object
 31  tg      521 non-null    object
 32  tn      521 non-null    object
 33  tr      521 non-null    object
 34  tt      521 non-null    object
 35  un      521 non-null    object
 36  up      521 non-null    object
 37  ut      521 non-null    object
 38  wb      521 non-null    object
dtypes: object(39)
memory usage: 162.8+ KB
In [20]:
df.tn
Out[20]:
date
2020-03-14       1
2020-03-15       0
2020-03-16       0
2020-03-17       0
2020-03-18       1
              ... 
2021-08-12    1942
2021-08-13    1933
2021-08-14    1916
2021-08-15    1896
2021-08-16    1851
Name: tn, Length: 521, dtype: object
In [21]:
pd.to_numeric(df.tn)
Out[21]:
date
2020-03-14       1
2020-03-15       0
2020-03-16       0
2020-03-17       0
2020-03-18       1
              ... 
2021-08-12    1942
2021-08-13    1933
2021-08-14    1916
2021-08-15    1896
2021-08-16    1851
Name: tn, Length: 521, dtype: int64
In [22]:
df = df.apply(pd.to_numeric)
In [23]:
df.info()
<class 'pandas.core.frame.DataFrame'>
DatetimeIndex: 521 entries, 2020-03-14 to 2021-08-16
Data columns (total 39 columns):
 #   Column  Non-Null Count  Dtype
---  ------  --------------  -----
 0   an      521 non-null    int64
 1   ap      521 non-null    int64
 2   ar      521 non-null    int64
 3   as      521 non-null    int64
 4   br      521 non-null    int64
 5   ch      521 non-null    int64
 6   ct      521 non-null    int64
 7   dd      521 non-null    int64
 8   dl      521 non-null    int64
 9   dn      521 non-null    int64
 10  ga      521 non-null    int64
 11  gj      521 non-null    int64
 12  hp      521 non-null    int64
 13  hr      521 non-null    int64
 14  jh      521 non-null    int64
 15  jk      521 non-null    int64
 16  ka      521 non-null    int64
 17  kl      521 non-null    int64
 18  la      521 non-null    int64
 19  ld      521 non-null    int64
 20  mh      521 non-null    int64
 21  ml      521 non-null    int64
 22  mn      521 non-null    int64
 23  mp      521 non-null    int64
 24  mz      521 non-null    int64
 25  nl      521 non-null    int64
 26  or      521 non-null    int64
 27  pb      521 non-null    int64
 28  py      521 non-null    int64
 29  rj      521 non-null    int64
 30  sk      521 non-null    int64
 31  tg      521 non-null    int64
 32  tn      521 non-null    int64
 33  tr      521 non-null    int64
 34  tt      521 non-null    int64
 35  un      521 non-null    int64
 36  up      521 non-null    int64
 37  ut      521 non-null    int64
 38  wb      521 non-null    int64
dtypes: int64(39)
memory usage: 162.8 KB
In [24]:
df.tail(7)
Out[24]:
an ap ar as br ch ct dd dl dn ... rj sk tg tn tr tt un up ut wb
date
2021-08-10 2 1461 233 929 44 8 112 0 52 1 ... 11 110 494 1893 205 38376 0 19 39 639
2021-08-11 0 1869 188 886 47 5 83 0 37 0 ... 19 157 482 1964 244 41586 0 24 40 700
2021-08-12 0 1859 180 935 43 12 98 0 49 1 ... 17 100 453 1942 140 40081 0 15 24 747
2021-08-13 0 1746 166 763 47 15 77 0 50 0 ... 24 150 427 1933 180 38761 0 25 27 739
2021-08-14 0 1535 161 755 39 4 83 0 50 0 ... 14 129 420 1916 137 36135 0 42 19 705
2021-08-15 0 1506 48 411 28 1 49 0 53 0 ... 18 152 245 1896 142 33245 0 30 16 673
2021-08-16 1 909 165 758 14 2 68 0 27 2 ... 11 20 405 1851 52 24696 0 17 18 502

7 rows × 39 columns

Styling tabulation

In [25]:
df7 = df.tail(7)
In [26]:
df7.style
Out[26]:
  an ap ar as br ch ct dd dl dn ga gj hp hr jh jk ka kl la ld mh ml mn mp mz nl or pb py rj sk tg tn tr tt un up ut wb
date                                                                              
2021-08-10 00:00:00 2 1461 233 929 44 8 112 0 52 1 141 21 419 23 34 113 1338 21119 7 6 5609 411 633 10 937 102 1041 59 101 11 110 494 1893 205 38376 0 19 39 639
2021-08-11 00:00:00 0 1869 188 886 47 5 83 0 37 0 103 16 374 16 14 114 1826 23500 10 5 5560 463 606 10 863 64 1078 105 114 19 157 482 1964 244 41586 0 24 40 700
2021-08-12 00:00:00 0 1859 180 935 43 12 98 0 49 1 88 17 354 16 44 176 1857 21445 9 6 6388 429 677 8 576 80 1107 80 109 17 100 453 1942 140 40081 0 15 24 747
2021-08-13 00:00:00 0 1746 166 763 47 15 77 0 50 0 67 23 333 26 32 129 1669 20452 6 4 6686 384 522 9 575 81 1193 88 113 24 150 427 1933 180 38761 0 25 27 739
2021-08-14 00:00:00 0 1535 161 755 39 4 83 0 50 0 88 25 284 14 28 135 1632 19451 9 7 5787 343 439 16 524 61 1132 50 101 14 129 420 1916 137 36135 0 42 19 705
2021-08-15 00:00:00 0 1506 48 411 28 1 49 0 53 0 75 16 182 22 27 97 1431 18582 6 3 4797 420 602 13 467 54 1058 46 79 18 152 245 1896 142 33245 0 30 16 673
2021-08-16 00:00:00 1 909 165 758 14 2 68 0 27 2 62 14 276 22 35 77 1065 12294 4 6 4145 352 275 7 249 44 868 30 49 11 20 405 1851 52 24696 0 17 18 502

color negative values in red

In [27]:
def color_red_negative(x):
    color = 'red' if x < 0 else 'white'
    return 'color: ' + color
In [30]:
df7.style.applymap(color_red_negative)
Out[30]:
  an ap ar as br ch ct dd dl dn ga gj hp hr jh jk ka kl la ld mh ml mn mp mz nl or pb py rj sk tg tn tr tt un up ut wb
date                                                                              
2021-08-10 00:00:00 2 1461 233 929 44 8 112 0 52 1 141 21 419 23 34 113 1338 21119 7 6 5609 411 633 10 937 102 1041 59 101 11 110 494 1893 205 38376 0 19 39 639
2021-08-11 00:00:00 0 1869 188 886 47 5 83 0 37 0 103 16 374 16 14 114 1826 23500 10 5 5560 463 606 10 863 64 1078 105 114 19 157 482 1964 244 41586 0 24 40 700
2021-08-12 00:00:00 0 1859 180 935 43 12 98 0 49 1 88 17 354 16 44 176 1857 21445 9 6 6388 429 677 8 576 80 1107 80 109 17 100 453 1942 140 40081 0 15 24 747
2021-08-13 00:00:00 0 1746 166 763 47 15 77 0 50 0 67 23 333 26 32 129 1669 20452 6 4 6686 384 522 9 575 81 1193 88 113 24 150 427 1933 180 38761 0 25 27 739
2021-08-14 00:00:00 0 1535 161 755 39 4 83 0 50 0 88 25 284 14 28 135 1632 19451 9 7 5787 343 439 16 524 61 1132 50 101 14 129 420 1916 137 36135 0 42 19 705
2021-08-15 00:00:00 0 1506 48 411 28 1 49 0 53 0 75 16 182 22 27 97 1431 18582 6 3 4797 420 602 13 467 54 1058 46 79 18 152 245 1896 142 33245 0 30 16 673
2021-08-16 00:00:00 1 909 165 758 14 2 68 0 27 2 62 14 276 22 35 77 1065 12294 4 6 4145 352 275 7 249 44 868 30 49 11 20 405 1851 52 24696 0 17 18 502
In [31]:
df7.drop('un', inplace = True, axis=1)
/usr/local/lib/python3.7/dist-packages/pandas/core/frame.py:4913: SettingWithCopyWarning: 
A value is trying to be set on a copy of a slice from a DataFrame

See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy
  errors=errors,
In [36]:
df7.style.highlight_max('red')
---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
/usr/local/lib/python3.7/dist-packages/IPython/core/formatters.py in __call__(self, obj)
    332                 pass
    333             else:
--> 334                 return printer(obj)
    335             # Finally look for special method names
    336             method = get_real_method(obj, self.print_method)

/usr/local/lib/python3.7/dist-packages/google/colab/_reprs.py in new_formatter(dataframe)
     86 
     87   def new_formatter(dataframe):
---> 88     return dataframe.set_table_attributes('class="dataframe"')._repr_html_()  # pylint: disable=protected-access
     89 
     90   _original_df_formatters[key] = formatters[key].for_type_by_name(

/usr/local/lib/python3.7/dist-packages/pandas/io/formats/style.py in _repr_html_(self)
    214         Hooks into Jupyter notebook rich display system.
    215         """
--> 216         return self.render()
    217 
    218     def render(

/usr/local/lib/python3.7/dist-packages/pandas/io/formats/style.py in render(self, sparse_index, sparse_columns, **kwargs)
    270         if sparse_columns is None:
    271             sparse_columns = get_option("styler.sparse.columns")
--> 272         return self._render_html(sparse_index, sparse_columns, **kwargs)
    273 
    274     def set_tooltips(

/usr/local/lib/python3.7/dist-packages/pandas/io/formats/style_render.py in _render_html(self, sparse_index, sparse_columns, **kwargs)
    119         Generates a dict with necessary kwargs passed to jinja2 template.
    120         """
--> 121         self._compute()
    122         # TODO: namespace all the pandas keys
    123         d = self._translate(sparse_index, sparse_columns)

/usr/local/lib/python3.7/dist-packages/pandas/io/formats/style_render.py in _compute(self)
    158         r = self
    159         for func, args, kwargs in self._todo:
--> 160             r = func(self)(*args, **kwargs)
    161         return r
    162 

/usr/local/lib/python3.7/dist-packages/pandas/io/formats/style.py in _apply(self, func, axis, subset, **kwargs)
   1051         subset = slice(None) if subset is None else subset
   1052         subset = non_reducing_slice(subset)
-> 1053         data = self.data.loc[subset]
   1054         if axis is not None:
   1055             result = data.apply(func, axis=axis, result_type="expand", **kwargs)

/usr/local/lib/python3.7/dist-packages/pandas/core/indexing.py in __getitem__(self, key)
    923                 with suppress(KeyError, IndexError):
    924                     return self.obj._get_value(*key, takeable=self._takeable)
--> 925             return self._getitem_tuple(key)
    926         else:
    927             # we by definition only have the 0th axis

/usr/local/lib/python3.7/dist-packages/pandas/core/indexing.py in _getitem_tuple(self, tup)
   1107             return self._multi_take(tup)
   1108 
-> 1109         return self._getitem_tuple_same_dim(tup)
   1110 
   1111     def _get_label(self, label, axis: int):

/usr/local/lib/python3.7/dist-packages/pandas/core/indexing.py in _getitem_tuple_same_dim(self, tup)
    804                 continue
    805 
--> 806             retval = getattr(retval, self.name)._getitem_axis(key, axis=i)
    807             # We should never have retval.ndim < self.ndim, as that should
    808             #  be handled by the _getitem_lowerdim call above.

/usr/local/lib/python3.7/dist-packages/pandas/core/indexing.py in _getitem_axis(self, key, axis)
   1151                     raise ValueError("Cannot index with multidimensional key")
   1152 
-> 1153                 return self._getitem_iterable(key, axis=axis)
   1154 
   1155             # nested tuple slicing

/usr/local/lib/python3.7/dist-packages/pandas/core/indexing.py in _getitem_iterable(self, key, axis)
   1091 
   1092         # A collection of keys
-> 1093         keyarr, indexer = self._get_listlike_indexer(key, axis)
   1094         return self.obj._reindex_with_indexers(
   1095             {axis: [keyarr, indexer]}, copy=True, allow_dups=True

/usr/local/lib/python3.7/dist-packages/pandas/core/indexing.py in _get_listlike_indexer(self, key, axis)
   1312             keyarr, indexer, new_indexer = ax._reindex_non_unique(keyarr)
   1313 
-> 1314         self._validate_read_indexer(keyarr, indexer, axis)
   1315 
   1316         if needs_i8_conversion(ax.dtype) or isinstance(

/usr/local/lib/python3.7/dist-packages/pandas/core/indexing.py in _validate_read_indexer(self, key, indexer, axis)
   1372                 if use_interval_msg:
   1373                     key = list(key)
-> 1374                 raise KeyError(f"None of [{key}] are in the [{axis_name}]")
   1375 
   1376             not_found = list(ensure_index(key)[missing_mask.nonzero()[0]].unique())

KeyError: "None of [Index(['red'], dtype='object')] are in the [columns]"
Out[36]:
<pandas.io.formats.style.Styler at 0x7f50318bcd90>
In [33]:
df7.style.highlight_max(color='red')
Out[33]:
  an ap ar as br ch ct dd dl dn ga gj hp hr jh jk ka kl la ld mh ml mn mp mz nl or pb py rj sk tg tn tr tt up ut wb
date                                                                            
2021-08-10 00:00:00 2 1461 233 929 44 8 112 0 52 1 141 21 419 23 34 113 1338 21119 7 6 5609 411 633 10 937 102 1041 59 101 11 110 494 1893 205 38376 19 39 639
2021-08-11 00:00:00 0 1869 188 886 47 5 83 0 37 0 103 16 374 16 14 114 1826 23500 10 5 5560 463 606 10 863 64 1078 105 114 19 157 482 1964 244 41586 24 40 700
2021-08-12 00:00:00 0 1859 180 935 43 12 98 0 49 1 88 17 354 16 44 176 1857 21445 9 6 6388 429 677 8 576 80 1107 80 109 17 100 453 1942 140 40081 15 24 747
2021-08-13 00:00:00 0 1746 166 763 47 15 77 0 50 0 67 23 333 26 32 129 1669 20452 6 4 6686 384 522 9 575 81 1193 88 113 24 150 427 1933 180 38761 25 27 739
2021-08-14 00:00:00 0 1535 161 755 39 4 83 0 50 0 88 25 284 14 28 135 1632 19451 9 7 5787 343 439 16 524 61 1132 50 101 14 129 420 1916 137 36135 42 19 705
2021-08-15 00:00:00 0 1506 48 411 28 1 49 0 53 0 75 16 182 22 27 97 1431 18582 6 3 4797 420 602 13 467 54 1058 46 79 18 152 245 1896 142 33245 30 16 673
2021-08-16 00:00:00 1 909 165 758 14 2 68 0 27 2 62 14 276 22 35 77 1065 12294 4 6 4145 352 275 7 249 44 868 30 49 11 20 405 1851 52 24696 17 18 502
In [128]:
df7.style.highlight_max(color='red')
Out[128]:
  an ap ar as br ch ct dl dn ga gj hp hr jh jk ka kl la ld mh ml mn mp mz nl or pb py rj sk tg tn tr up ut wb
date                                                                        
2021-08-10 00:00:00 2 1461 233 929 44 8 112 52 1 141 21 419 23 34 113 1338 21119 7 6 5609 411 633 10 937 102 1041 59 101 11 110 494 1893 205 19 39 639
2021-08-11 00:00:00 0 1869 188 886 47 5 83 37 0 103 16 374 16 14 114 1826 23500 10 5 5560 463 606 10 863 64 1078 105 114 19 157 482 1964 244 24 40 700
2021-08-12 00:00:00 0 1859 180 935 43 12 98 49 1 88 17 354 16 44 176 1857 21445 9 6 6388 429 677 8 576 80 1107 80 109 17 100 453 1942 140 15 24 747
2021-08-13 00:00:00 0 1746 166 763 47 15 77 50 0 67 23 333 26 32 129 1669 20452 6 4 6686 384 522 9 575 81 1193 88 113 24 150 427 1933 180 25 27 739
2021-08-14 00:00:00 0 1535 161 755 39 4 83 50 0 88 25 284 14 28 135 1632 19451 9 7 5787 343 439 16 524 61 1132 50 101 14 129 420 1916 137 42 19 705
2021-08-15 00:00:00 0 1506 48 411 28 1 49 53 0 75 16 182 22 27 97 1431 18582 6 3 4797 420 602 13 467 54 1058 46 79 18 152 245 1896 142 30 16 673
2021-08-16 00:00:00 1 909 165 758 14 2 68 27 2 62 14 276 22 35 77 1065 12294 4 6 4145 352 275 7 249 44 868 30 49 11 20 405 1851 52 17 18 502
In [ ]:
df7.drop(['dd', 'ld'], axis=1, inplace=True)
/usr/local/lib/python3.7/dist-packages/pandas/core/frame.py:4913: SettingWithCopyWarning: 
A value is trying to be set on a copy of a slice from a DataFrame

See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy
  errors=errors,
In [ ]:
df7.style.highlight_max(color='red').highlight_min(color='green')
Out[ ]:
  an ap ar as br ch ct dl dn ga gj hp hr jh jk ka kl la mh ml mn mp mz nl or pb py rj sk tg tn tr tt up ut wb
date                                                                        
2021-08-10 00:00:00 2 1461 233 929 44 8 112 52 1 141 21 419 23 34 113 1338 21119 7 5609 411 633 10 937 102 1041 59 101 11 110 494 1893 205 38376 19 39 639
2021-08-11 00:00:00 0 1869 188 886 47 5 83 37 0 103 16 374 16 14 114 1826 23500 10 5560 463 606 10 863 64 1078 105 114 19 157 482 1964 244 41586 24 40 700
2021-08-12 00:00:00 0 1859 180 935 43 12 98 49 1 88 17 354 16 44 176 1857 21445 9 6388 429 677 8 576 80 1107 80 109 17 100 453 1942 140 40081 15 24 747
2021-08-13 00:00:00 0 1746 166 763 47 15 77 50 0 67 23 333 26 32 129 1669 20452 6 6686 384 522 9 575 81 1193 88 113 24 150 427 1933 180 38761 25 27 739
2021-08-14 00:00:00 0 1535 161 755 39 4 83 50 0 88 25 284 14 28 135 1632 19451 9 5787 343 439 16 524 61 1132 50 101 14 129 420 1916 137 36135 42 19 705
2021-08-15 00:00:00 0 1506 48 411 28 1 49 53 0 75 16 182 22 27 97 1431 18582 6 4797 420 602 13 467 54 1058 46 79 18 152 245 1896 142 33245 30 16 673
2021-08-16 00:00:00 1 909 165 758 14 2 68 27 2 62 14 276 22 35 77 1065 12294 4 4145 352 275 7 249 44 868 30 49 11 20 405 1851 52 24696 17 18 502
In [35]:
df7.drop('tt', axis = 1,inplace = True)
/usr/local/lib/python3.7/dist-packages/pandas/core/frame.py:4913: SettingWithCopyWarning: 
A value is trying to be set on a copy of a slice from a DataFrame

See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy
  errors=errors,
In [45]:
def color_cyan_negative(x):
    color = 'cyan' if x == 0 else 'while'
    return 'color:' + color
In [46]:
df7.style.applymap(color_cyan_negative)
Out[46]:
  an ap ar as br ch ct dd dl dn ga gj hp hr jh jk ka kl la ld mh ml mn mp mz nl or pb py rj sk tg tn tr up ut wb
date                                                                          
2021-08-10 00:00:00 2 1461 233 929 44 8 112 0 52 1 141 21 419 23 34 113 1338 21119 7 6 5609 411 633 10 937 102 1041 59 101 11 110 494 1893 205 19 39 639
2021-08-11 00:00:00 0 1869 188 886 47 5 83 0 37 0 103 16 374 16 14 114 1826 23500 10 5 5560 463 606 10 863 64 1078 105 114 19 157 482 1964 244 24 40 700
2021-08-12 00:00:00 0 1859 180 935 43 12 98 0 49 1 88 17 354 16 44 176 1857 21445 9 6 6388 429 677 8 576 80 1107 80 109 17 100 453 1942 140 15 24 747
2021-08-13 00:00:00 0 1746 166 763 47 15 77 0 50 0 67 23 333 26 32 129 1669 20452 6 4 6686 384 522 9 575 81 1193 88 113 24 150 427 1933 180 25 27 739
2021-08-14 00:00:00 0 1535 161 755 39 4 83 0 50 0 88 25 284 14 28 135 1632 19451 9 7 5787 343 439 16 524 61 1132 50 101 14 129 420 1916 137 42 19 705
2021-08-15 00:00:00 0 1506 48 411 28 1 49 0 53 0 75 16 182 22 27 97 1431 18582 6 3 4797 420 602 13 467 54 1058 46 79 18 152 245 1896 142 30 16 673
2021-08-16 00:00:00 1 909 165 758 14 2 68 0 27 2 62 14 276 22 35 77 1065 12294 4 6 4145 352 275 7 249 44 868 30 49 11 20 405 1851 52 17 18 502
In [49]:
df7.style.highlight_max(color='green')
Out[49]:
  an ap ar as br ch ct dd dl dn ga gj hp hr jh jk ka kl la ld mh ml mn mp mz nl or pb py rj sk tg tn tr up ut wb
date                                                                          
2021-08-10 00:00:00 2 1461 233 929 44 8 112 0 52 1 141 21 419 23 34 113 1338 21119 7 6 5609 411 633 10 937 102 1041 59 101 11 110 494 1893 205 19 39 639
2021-08-11 00:00:00 0 1869 188 886 47 5 83 0 37 0 103 16 374 16 14 114 1826 23500 10 5 5560 463 606 10 863 64 1078 105 114 19 157 482 1964 244 24 40 700
2021-08-12 00:00:00 0 1859 180 935 43 12 98 0 49 1 88 17 354 16 44 176 1857 21445 9 6 6388 429 677 8 576 80 1107 80 109 17 100 453 1942 140 15 24 747
2021-08-13 00:00:00 0 1746 166 763 47 15 77 0 50 0 67 23 333 26 32 129 1669 20452 6 4 6686 384 522 9 575 81 1193 88 113 24 150 427 1933 180 25 27 739
2021-08-14 00:00:00 0 1535 161 755 39 4 83 0 50 0 88 25 284 14 28 135 1632 19451 9 7 5787 343 439 16 524 61 1132 50 101 14 129 420 1916 137 42 19 705
2021-08-15 00:00:00 0 1506 48 411 28 1 49 0 53 0 75 16 182 22 27 97 1431 18582 6 3 4797 420 602 13 467 54 1058 46 79 18 152 245 1896 142 30 16 673
2021-08-16 00:00:00 1 909 165 758 14 2 68 0 27 2 62 14 276 22 35 77 1065 12294 4 6 4145 352 275 7 249 44 868 30 49 11 20 405 1851 52 17 18 502
In [51]:
df7.drop('dd', inplace = True, axis=1)
/usr/local/lib/python3.7/dist-packages/pandas/core/frame.py:4913: SettingWithCopyWarning: 
A value is trying to be set on a copy of a slice from a DataFrame

See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy
  errors=errors,
In [52]:
df7
Out[52]:
an ap ar as br ch ct dl dn ga ... pb py rj sk tg tn tr up ut wb
date
2021-08-10 2 1461 233 929 44 8 112 52 1 141 ... 59 101 11 110 494 1893 205 19 39 639
2021-08-11 0 1869 188 886 47 5 83 37 0 103 ... 105 114 19 157 482 1964 244 24 40 700
2021-08-12 0 1859 180 935 43 12 98 49 1 88 ... 80 109 17 100 453 1942 140 15 24 747
2021-08-13 0 1746 166 763 47 15 77 50 0 67 ... 88 113 24 150 427 1933 180 25 27 739
2021-08-14 0 1535 161 755 39 4 83 50 0 88 ... 50 101 14 129 420 1916 137 42 19 705
2021-08-15 0 1506 48 411 28 1 49 53 0 75 ... 46 79 18 152 245 1896 142 30 16 673
2021-08-16 1 909 165 758 14 2 68 27 2 62 ... 30 49 11 20 405 1851 52 17 18 502

7 rows × 36 columns

In [56]:
df7.style.highlight_max(color='red')
Out[56]:
  an ap ar as br ch ct dl dn ga gj hp hr jh jk ka kl la ld mh ml mn mp mz nl or pb py rj sk tg tn tr up ut wb
date                                                                        
2021-08-10 00:00:00 2 1461 233 929 44 8 112 52 1 141 21 419 23 34 113 1338 21119 7 6 5609 411 633 10 937 102 1041 59 101 11 110 494 1893 205 19 39 639
2021-08-11 00:00:00 0 1869 188 886 47 5 83 37 0 103 16 374 16 14 114 1826 23500 10 5 5560 463 606 10 863 64 1078 105 114 19 157 482 1964 244 24 40 700
2021-08-12 00:00:00 0 1859 180 935 43 12 98 49 1 88 17 354 16 44 176 1857 21445 9 6 6388 429 677 8 576 80 1107 80 109 17 100 453 1942 140 15 24 747
2021-08-13 00:00:00 0 1746 166 763 47 15 77 50 0 67 23 333 26 32 129 1669 20452 6 4 6686 384 522 9 575 81 1193 88 113 24 150 427 1933 180 25 27 739
2021-08-14 00:00:00 0 1535 161 755 39 4 83 50 0 88 25 284 14 28 135 1632 19451 9 7 5787 343 439 16 524 61 1132 50 101 14 129 420 1916 137 42 19 705
2021-08-15 00:00:00 0 1506 48 411 28 1 49 53 0 75 16 182 22 27 97 1431 18582 6 3 4797 420 602 13 467 54 1058 46 79 18 152 245 1896 142 30 16 673
2021-08-16 00:00:00 1 909 165 758 14 2 68 27 2 62 14 276 22 35 77 1065 12294 4 6 4145 352 275 7 249 44 868 30 49 11 20 405 1851 52 17 18 502
In [57]:
df7.style.highlight_min(color='green')
Out[57]:
  an ap ar as br ch ct dl dn ga gj hp hr jh jk ka kl la ld mh ml mn mp mz nl or pb py rj sk tg tn tr up ut wb
date                                                                        
2021-08-10 00:00:00 2 1461 233 929 44 8 112 52 1 141 21 419 23 34 113 1338 21119 7 6 5609 411 633 10 937 102 1041 59 101 11 110 494 1893 205 19 39 639
2021-08-11 00:00:00 0 1869 188 886 47 5 83 37 0 103 16 374 16 14 114 1826 23500 10 5 5560 463 606 10 863 64 1078 105 114 19 157 482 1964 244 24 40 700
2021-08-12 00:00:00 0 1859 180 935 43 12 98 49 1 88 17 354 16 44 176 1857 21445 9 6 6388 429 677 8 576 80 1107 80 109 17 100 453 1942 140 15 24 747
2021-08-13 00:00:00 0 1746 166 763 47 15 77 50 0 67 23 333 26 32 129 1669 20452 6 4 6686 384 522 9 575 81 1193 88 113 24 150 427 1933 180 25 27 739
2021-08-14 00:00:00 0 1535 161 755 39 4 83 50 0 88 25 284 14 28 135 1632 19451 9 7 5787 343 439 16 524 61 1132 50 101 14 129 420 1916 137 42 19 705
2021-08-15 00:00:00 0 1506 48 411 28 1 49 53 0 75 16 182 22 27 97 1431 18582 6 3 4797 420 602 13 467 54 1058 46 79 18 152 245 1896 142 30 16 673
2021-08-16 00:00:00 1 909 165 758 14 2 68 27 2 62 14 276 22 35 77 1065 12294 4 6 4145 352 275 7 249 44 868 30 49 11 20 405 1851 52 17 18 502

highlight both high and low values in red and green respectively

In [58]:
df7.style.highlight_max(color='red').highlight_min(color='green')
Out[58]:
  an ap ar as br ch ct dl dn ga gj hp hr jh jk ka kl la ld mh ml mn mp mz nl or pb py rj sk tg tn tr up ut wb
date                                                                        
2021-08-10 00:00:00 2 1461 233 929 44 8 112 52 1 141 21 419 23 34 113 1338 21119 7 6 5609 411 633 10 937 102 1041 59 101 11 110 494 1893 205 19 39 639
2021-08-11 00:00:00 0 1869 188 886 47 5 83 37 0 103 16 374 16 14 114 1826 23500 10 5 5560 463 606 10 863 64 1078 105 114 19 157 482 1964 244 24 40 700
2021-08-12 00:00:00 0 1859 180 935 43 12 98 49 1 88 17 354 16 44 176 1857 21445 9 6 6388 429 677 8 576 80 1107 80 109 17 100 453 1942 140 15 24 747
2021-08-13 00:00:00 0 1746 166 763 47 15 77 50 0 67 23 333 26 32 129 1669 20452 6 4 6686 384 522 9 575 81 1193 88 113 24 150 427 1933 180 25 27 739
2021-08-14 00:00:00 0 1535 161 755 39 4 83 50 0 88 25 284 14 28 135 1632 19451 9 7 5787 343 439 16 524 61 1132 50 101 14 129 420 1916 137 42 19 705
2021-08-15 00:00:00 0 1506 48 411 28 1 49 53 0 75 16 182 22 27 97 1431 18582 6 3 4797 420 602 13 467 54 1058 46 79 18 152 245 1896 142 30 16 673
2021-08-16 00:00:00 1 909 165 758 14 2 68 27 2 62 14 276 22 35 77 1065 12294 4 6 4145 352 275 7 249 44 868 30 49 11 20 405 1851 52 17 18 502

Write a function to find the max value and make it bold

  • apply() is used to apply a function along an axis or column of the DataFrame or on values of Series.
  • applymap() is used to apply a function to each element in a DataFrame.
  • map() is used to substitute each value in a Series with another value
In [78]:
df7
Out[78]:
an ap ar as br ch ct dl dn ga ... pb py rj sk tg tn tr up ut wb
date
2021-08-10 2 1461 233 929 44 8 112 52 1 141 ... 59 101 11 110 494 1893 205 19 39 639
2021-08-11 0 1869 188 886 47 5 83 37 0 103 ... 105 114 19 157 482 1964 244 24 40 700
2021-08-12 0 1859 180 935 43 12 98 49 1 88 ... 80 109 17 100 453 1942 140 15 24 747
2021-08-13 0 1746 166 763 47 15 77 50 0 67 ... 88 113 24 150 427 1933 180 25 27 739
2021-08-14 0 1535 161 755 39 4 83 50 0 88 ... 50 101 14 129 420 1916 137 42 19 705
2021-08-15 0 1506 48 411 28 1 49 53 0 75 ... 46 79 18 152 245 1896 142 30 16 673
2021-08-16 1 909 165 758 14 2 68 27 2 62 ... 30 49 11 20 405 1851 52 17 18 502

7 rows × 36 columns

In [84]:
def print_max(x):
    is_max = (x == x.max())
    print(is_max)
In [ ]:
df7.apply(print_max)
In [89]:
def make_max_bold(x):
    is_max = (x == x.max())
    return ['font-weight: bold' if y else "" for y in is_max]
In [77]:
df7.style.apply(make_max_bold)
Out[77]:
  an ap ar as br ch ct dl dn ga gj hp hr jh jk ka kl la ld mh ml mn mp mz nl or pb py rj sk tg tn tr up ut wb
date                                                                        
2021-08-10 00:00:00 2 1461 233 929 44 8 112 52 1 141 21 419 23 34 113 1338 21119 7 6 5609 411 633 10 937 102 1041 59 101 11 110 494 1893 205 19 39 639
2021-08-11 00:00:00 0 1869 188 886 47 5 83 37 0 103 16 374 16 14 114 1826 23500 10 5 5560 463 606 10 863 64 1078 105 114 19 157 482 1964 244 24 40 700
2021-08-12 00:00:00 0 1859 180 935 43 12 98 49 1 88 17 354 16 44 176 1857 21445 9 6 6388 429 677 8 576 80 1107 80 109 17 100 453 1942 140 15 24 747
2021-08-13 00:00:00 0 1746 166 763 47 15 77 50 0 67 23 333 26 32 129 1669 20452 6 4 6686 384 522 9 575 81 1193 88 113 24 150 427 1933 180 25 27 739
2021-08-14 00:00:00 0 1535 161 755 39 4 83 50 0 88 25 284 14 28 135 1632 19451 9 7 5787 343 439 16 524 61 1132 50 101 14 129 420 1916 137 42 19 705
2021-08-15 00:00:00 0 1506 48 411 28 1 49 53 0 75 16 182 22 27 97 1431 18582 6 3 4797 420 602 13 467 54 1058 46 79 18 152 245 1896 142 30 16 673
2021-08-16 00:00:00 1 909 165 758 14 2 68 27 2 62 14 276 22 35 77 1065 12294 4 6 4145 352 275 7 249 44 868 30 49 11 20 405 1851 52 17 18 502
In [74]:
df7.style.apply(make_max_bold).highlight_min(color='green', axis=0)
Out[74]:
  an ap ar as br ch ct dl dn ga gj hp hr jh jk ka kl la ld mh ml mn mp mz nl or pb py rj sk tg tn tr up ut wb
date                                                                        
2021-08-10 00:00:00 2 1461 233 929 44 8 112 52 1 141 21 419 23 34 113 1338 21119 7 6 5609 411 633 10 937 102 1041 59 101 11 110 494 1893 205 19 39 639
2021-08-11 00:00:00 0 1869 188 886 47 5 83 37 0 103 16 374 16 14 114 1826 23500 10 5 5560 463 606 10 863 64 1078 105 114 19 157 482 1964 244 24 40 700
2021-08-12 00:00:00 0 1859 180 935 43 12 98 49 1 88 17 354 16 44 176 1857 21445 9 6 6388 429 677 8 576 80 1107 80 109 17 100 453 1942 140 15 24 747
2021-08-13 00:00:00 0 1746 166 763 47 15 77 50 0 67 23 333 26 32 129 1669 20452 6 4 6686 384 522 9 575 81 1193 88 113 24 150 427 1933 180 25 27 739
2021-08-14 00:00:00 0 1535 161 755 39 4 83 50 0 88 25 284 14 28 135 1632 19451 9 7 5787 343 439 16 524 61 1132 50 101 14 129 420 1916 137 42 19 705
2021-08-15 00:00:00 0 1506 48 411 28 1 49 53 0 75 16 182 22 27 97 1431 18582 6 3 4797 420 602 13 467 54 1058 46 79 18 152 245 1896 142 30 16 673
2021-08-16 00:00:00 1 909 165 758 14 2 68 27 2 62 14 276 22 35 77 1065 12294 4 6 4145 352 275 7 249 44 868 30 49 11 20 405 1851 52 17 18 502
In [76]:
df7.style.apply(make_max_bold).highlight_max(color='green', axis=1)
Out[76]:
  an ap ar as br ch ct dl dn ga gj hp hr jh jk ka kl la ld mh ml mn mp mz nl or pb py rj sk tg tn tr up ut wb
date                                                                        
2021-08-10 00:00:00 2 1461 233 929 44 8 112 52 1 141 21 419 23 34 113 1338 21119 7 6 5609 411 633 10 937 102 1041 59 101 11 110 494 1893 205 19 39 639
2021-08-11 00:00:00 0 1869 188 886 47 5 83 37 0 103 16 374 16 14 114 1826 23500 10 5 5560 463 606 10 863 64 1078 105 114 19 157 482 1964 244 24 40 700
2021-08-12 00:00:00 0 1859 180 935 43 12 98 49 1 88 17 354 16 44 176 1857 21445 9 6 6388 429 677 8 576 80 1107 80 109 17 100 453 1942 140 15 24 747
2021-08-13 00:00:00 0 1746 166 763 47 15 77 50 0 67 23 333 26 32 129 1669 20452 6 4 6686 384 522 9 575 81 1193 88 113 24 150 427 1933 180 25 27 739
2021-08-14 00:00:00 0 1535 161 755 39 4 83 50 0 88 25 284 14 28 135 1632 19451 9 7 5787 343 439 16 524 61 1132 50 101 14 129 420 1916 137 42 19 705
2021-08-15 00:00:00 0 1506 48 411 28 1 49 53 0 75 16 182 22 27 97 1431 18582 6 3 4797 420 602 13 467 54 1058 46 79 18 152 245 1896 142 30 16 673
2021-08-16 00:00:00 1 909 165 758 14 2 68 27 2 62 14 276 22 35 77 1065 12294 4 6 4145 352 275 7 249 44 868 30 49 11 20 405 1851 52 17 18 502
In [94]:
df7.style.apply(make_max_bold).highlight_max(color='red', axis=1)
Out[94]:
  an ap ar as br ch ct dl dn ga gj hp hr jh jk ka kl la ld mh ml mn mp mz nl or pb py rj sk tg tn tr up ut wb
date                                                                        
2021-08-10 00:00:00 2 1461 233 929 44 8 112 52 1 141 21 419 23 34 113 1338 21119 7 6 5609 411 633 10 937 102 1041 59 101 11 110 494 1893 205 19 39 639
2021-08-11 00:00:00 0 1869 188 886 47 5 83 37 0 103 16 374 16 14 114 1826 23500 10 5 5560 463 606 10 863 64 1078 105 114 19 157 482 1964 244 24 40 700
2021-08-12 00:00:00 0 1859 180 935 43 12 98 49 1 88 17 354 16 44 176 1857 21445 9 6 6388 429 677 8 576 80 1107 80 109 17 100 453 1942 140 15 24 747
2021-08-13 00:00:00 0 1746 166 763 47 15 77 50 0 67 23 333 26 32 129 1669 20452 6 4 6686 384 522 9 575 81 1193 88 113 24 150 427 1933 180 25 27 739
2021-08-14 00:00:00 0 1535 161 755 39 4 83 50 0 88 25 284 14 28 135 1632 19451 9 7 5787 343 439 16 524 61 1132 50 101 14 129 420 1916 137 42 19 705
2021-08-15 00:00:00 0 1506 48 411 28 1 49 53 0 75 16 182 22 27 97 1431 18582 6 3 4797 420 602 13 467 54 1058 46 79 18 152 245 1896 142 30 16 673
2021-08-16 00:00:00 1 909 165 758 14 2 68 27 2 62 14 276 22 35 77 1065 12294 4 6 4145 352 275 7 249 44 868 30 49 11 20 405 1851 52 17 18 502
In [97]:
df7.style.background_gradient(cmap = 'Blues')
Out[97]:
  an ap ar as br ch ct dl dn ga gj hp hr jh jk ka kl la ld mh ml mn mp mz nl or pb py rj sk tg tn tr up ut wb
date                                                                        
2021-08-10 00:00:00 2 1461 233 929 44 8 112 52 1 141 21 419 23 34 113 1338 21119 7 6 5609 411 633 10 937 102 1041 59 101 11 110 494 1893 205 19 39 639
2021-08-11 00:00:00 0 1869 188 886 47 5 83 37 0 103 16 374 16 14 114 1826 23500 10 5 5560 463 606 10 863 64 1078 105 114 19 157 482 1964 244 24 40 700
2021-08-12 00:00:00 0 1859 180 935 43 12 98 49 1 88 17 354 16 44 176 1857 21445 9 6 6388 429 677 8 576 80 1107 80 109 17 100 453 1942 140 15 24 747
2021-08-13 00:00:00 0 1746 166 763 47 15 77 50 0 67 23 333 26 32 129 1669 20452 6 4 6686 384 522 9 575 81 1193 88 113 24 150 427 1933 180 25 27 739
2021-08-14 00:00:00 0 1535 161 755 39 4 83 50 0 88 25 284 14 28 135 1632 19451 9 7 5787 343 439 16 524 61 1132 50 101 14 129 420 1916 137 42 19 705
2021-08-15 00:00:00 0 1506 48 411 28 1 49 53 0 75 16 182 22 27 97 1431 18582 6 3 4797 420 602 13 467 54 1058 46 79 18 152 245 1896 142 30 16 673
2021-08-16 00:00:00 1 909 165 758 14 2 68 27 2 62 14 276 22 35 77 1065 12294 4 6 4145 352 275 7 249 44 868 30 49 11 20 405 1851 52 17 18 502

displays the list of all the valid values for cmap

  • light - denoting small number and
  • dark color - denoting large number
In [98]:
df7.style.background_gradient(cmap = 'blue')
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
/usr/local/lib/python3.7/dist-packages/IPython/core/formatters.py in __call__(self, obj)
    332                 pass
    333             else:
--> 334                 return printer(obj)
    335             # Finally look for special method names
    336             method = get_real_method(obj, self.print_method)

/usr/local/lib/python3.7/dist-packages/google/colab/_reprs.py in new_formatter(dataframe)
     86 
     87   def new_formatter(dataframe):
---> 88     return dataframe.set_table_attributes('class="dataframe"')._repr_html_()  # pylint: disable=protected-access
     89 
     90   _original_df_formatters[key] = formatters[key].for_type_by_name(

/usr/local/lib/python3.7/dist-packages/pandas/io/formats/style.py in _repr_html_(self)
    214         Hooks into Jupyter notebook rich display system.
    215         """
--> 216         return self.render()
    217 
    218     def render(

/usr/local/lib/python3.7/dist-packages/pandas/io/formats/style.py in render(self, sparse_index, sparse_columns, **kwargs)
    270         if sparse_columns is None:
    271             sparse_columns = get_option("styler.sparse.columns")
--> 272         return self._render_html(sparse_index, sparse_columns, **kwargs)
    273 
    274     def set_tooltips(

/usr/local/lib/python3.7/dist-packages/pandas/io/formats/style_render.py in _render_html(self, sparse_index, sparse_columns, **kwargs)
    119         Generates a dict with necessary kwargs passed to jinja2 template.
    120         """
--> 121         self._compute()
    122         # TODO: namespace all the pandas keys
    123         d = self._translate(sparse_index, sparse_columns)

/usr/local/lib/python3.7/dist-packages/pandas/io/formats/style_render.py in _compute(self)
    158         r = self
    159         for func, args, kwargs in self._todo:
--> 160             r = func(self)(*args, **kwargs)
    161         return r
    162 

/usr/local/lib/python3.7/dist-packages/pandas/io/formats/style.py in _apply(self, func, axis, subset, **kwargs)
   1053         data = self.data.loc[subset]
   1054         if axis is not None:
-> 1055             result = data.apply(func, axis=axis, result_type="expand", **kwargs)
   1056             result.columns = data.columns
   1057         else:

/usr/local/lib/python3.7/dist-packages/pandas/core/frame.py in apply(self, func, axis, raw, result_type, args, **kwargs)
   8738             kwargs=kwargs,
   8739         )
-> 8740         return op.apply()
   8741 
   8742     def applymap(

/usr/local/lib/python3.7/dist-packages/pandas/core/apply.py in apply(self)
    686             return self.apply_raw()
    687 
--> 688         return self.apply_standard()
    689 
    690     def agg(self):

/usr/local/lib/python3.7/dist-packages/pandas/core/apply.py in apply_standard(self)
    810 
    811     def apply_standard(self):
--> 812         results, res_index = self.apply_series_generator()
    813 
    814         # wrap results

/usr/local/lib/python3.7/dist-packages/pandas/core/apply.py in apply_series_generator(self)
    826             for i, v in enumerate(series_gen):
    827                 # ignore SettingWithCopy here in case the user mutates
--> 828                 results[i] = self.f(v)
    829                 if isinstance(results[i], ABCSeries):
    830                     # If we have a view on v, we need to make a copy because

/usr/local/lib/python3.7/dist-packages/pandas/core/apply.py in f(x)
    129 
    130             def f(x):
--> 131                 return func(x, *args, **kwargs)
    132 
    133         else:

/usr/local/lib/python3.7/dist-packages/pandas/io/formats/style.py in _background_gradient(data, cmap, low, high, text_color_threshold, vmin, vmax, gmap, text_only)
   2816         # extend lower / upper bounds, compresses color range
   2817         norm = colors.Normalize(smin - (rng * low), smax + (rng * high))
-> 2818         rgbas = plt.cm.get_cmap(cmap)(norm(gmap))
   2819 
   2820         def relative_luminance(rgba) -> float:

/usr/local/lib/python3.7/dist-packages/matplotlib/cm.py in get_cmap(name, lut)
    132     if isinstance(name, colors.Colormap):
    133         return name
--> 134     cbook._check_in_list(sorted(cmap_d), name=name)
    135     if lut is None:
    136         return cmap_d[name]

/usr/local/lib/python3.7/dist-packages/matplotlib/cbook/__init__.py in _check_in_list(_values, **kwargs)
   2143             raise ValueError(
   2144                 "{!r} is not a valid value for {}; supported values are {}"
-> 2145                 .format(v, k, ', '.join(map(repr, values))))
   2146 
   2147 

ValueError: 'blue' is not a valid value for name; supported values are 'Accent', 'Accent_r', 'Blues', 'Blues_r', 'BrBG', 'BrBG_r', 'BuGn', 'BuGn_r', 'BuPu', 'BuPu_r', 'CMRmap', 'CMRmap_r', 'Dark2', 'Dark2_r', 'GnBu', 'GnBu_r', 'Greens', 'Greens_r', 'Greys', 'Greys_r', 'OrRd', 'OrRd_r', 'Oranges', 'Oranges_r', 'PRGn', 'PRGn_r', 'Paired', 'Paired_r', 'Pastel1', 'Pastel1_r', 'Pastel2', 'Pastel2_r', 'PiYG', 'PiYG_r', 'PuBu', 'PuBuGn', 'PuBuGn_r', 'PuBu_r', 'PuOr', 'PuOr_r', 'PuRd', 'PuRd_r', 'Purples', 'Purples_r', 'RdBu', 'RdBu_r', 'RdGy', 'RdGy_r', 'RdPu', 'RdPu_r', 'RdYlBu', 'RdYlBu_r', 'RdYlGn', 'RdYlGn_r', 'Reds', 'Reds_r', 'Set1', 'Set1_r', 'Set2', 'Set2_r', 'Set3', 'Set3_r', 'Spectral', 'Spectral_r', 'Wistia', 'Wistia_r', 'YlGn', 'YlGnBu', 'YlGnBu_r', 'YlGn_r', 'YlOrBr', 'YlOrBr_r', 'YlOrRd', 'YlOrRd_r', 'afmhot', 'afmhot_r', 'autumn', 'autumn_r', 'binary', 'binary_r', 'bone', 'bone_r', 'brg', 'brg_r', 'bwr', 'bwr_r', 'cividis', 'cividis_r', 'cool', 'cool_r', 'coolwarm', 'coolwarm_r', 'copper', 'copper_r', 'crest', 'crest_r', 'cubehelix', 'cubehelix_r', 'flag', 'flag_r', 'flare', 'flare_r', 'gist_earth', 'gist_earth_r', 'gist_gray', 'gist_gray_r', 'gist_heat', 'gist_heat_r', 'gist_ncar', 'gist_ncar_r', 'gist_rainbow', 'gist_rainbow_r', 'gist_stern', 'gist_stern_r', 'gist_yarg', 'gist_yarg_r', 'gnuplot', 'gnuplot2', 'gnuplot2_r', 'gnuplot_r', 'gray', 'gray_r', 'hot', 'hot_r', 'hsv', 'hsv_r', 'icefire', 'icefire_r', 'inferno', 'inferno_r', 'jet', 'jet_r', 'magm...
Out[98]:
<pandas.io.formats.style.Styler at 0x7f502c72a410>
In [103]:
df7.style.background_gradient(cmap = 'crest')
Out[103]:
  an ap ar as br ch ct dl dn ga gj hp hr jh jk ka kl la ld mh ml mn mp mz nl or pb py rj sk tg tn tr up ut wb
date                                                                        
2021-08-10 00:00:00 2 1461 233 929 44 8 112 52 1 141 21 419 23 34 113 1338 21119 7 6 5609 411 633 10 937 102 1041 59 101 11 110 494 1893 205 19 39 639
2021-08-11 00:00:00 0 1869 188 886 47 5 83 37 0 103 16 374 16 14 114 1826 23500 10 5 5560 463 606 10 863 64 1078 105 114 19 157 482 1964 244 24 40 700
2021-08-12 00:00:00 0 1859 180 935 43 12 98 49 1 88 17 354 16 44 176 1857 21445 9 6 6388 429 677 8 576 80 1107 80 109 17 100 453 1942 140 15 24 747
2021-08-13 00:00:00 0 1746 166 763 47 15 77 50 0 67 23 333 26 32 129 1669 20452 6 4 6686 384 522 9 575 81 1193 88 113 24 150 427 1933 180 25 27 739
2021-08-14 00:00:00 0 1535 161 755 39 4 83 50 0 88 25 284 14 28 135 1632 19451 9 7 5787 343 439 16 524 61 1132 50 101 14 129 420 1916 137 42 19 705
2021-08-15 00:00:00 0 1506 48 411 28 1 49 53 0 75 16 182 22 27 97 1431 18582 6 3 4797 420 602 13 467 54 1058 46 79 18 152 245 1896 142 30 16 673
2021-08-16 00:00:00 1 909 165 758 14 2 68 27 2 62 14 276 22 35 77 1065 12294 4 6 4145 352 275 7 249 44 868 30 49 11 20 405 1851 52 17 18 502
In [105]:
df7.style.background_gradient(cmap = "Reds", axis = 1)
Out[105]:
  an ap ar as br ch ct dl dn ga gj hp hr jh jk ka kl la ld mh ml mn mp mz nl or pb py rj sk tg tn tr up ut wb
date                                                                        
2021-08-10 00:00:00 2 1461 233 929 44 8 112 52 1 141 21 419 23 34 113 1338 21119 7 6 5609 411 633 10 937 102 1041 59 101 11 110 494 1893 205 19 39 639
2021-08-11 00:00:00 0 1869 188 886 47 5 83 37 0 103 16 374 16 14 114 1826 23500 10 5 5560 463 606 10 863 64 1078 105 114 19 157 482 1964 244 24 40 700
2021-08-12 00:00:00 0 1859 180 935 43 12 98 49 1 88 17 354 16 44 176 1857 21445 9 6 6388 429 677 8 576 80 1107 80 109 17 100 453 1942 140 15 24 747
2021-08-13 00:00:00 0 1746 166 763 47 15 77 50 0 67 23 333 26 32 129 1669 20452 6 4 6686 384 522 9 575 81 1193 88 113 24 150 427 1933 180 25 27 739
2021-08-14 00:00:00 0 1535 161 755 39 4 83 50 0 88 25 284 14 28 135 1632 19451 9 7 5787 343 439 16 524 61 1132 50 101 14 129 420 1916 137 42 19 705
2021-08-15 00:00:00 0 1506 48 411 28 1 49 53 0 75 16 182 22 27 97 1431 18582 6 3 4797 420 602 13 467 54 1058 46 79 18 152 245 1896 142 30 16 673
2021-08-16 00:00:00 1 909 165 758 14 2 68 27 2 62 14 276 22 35 77 1065 12294 4 6 4145 352 275 7 249 44 868 30 49 11 20 405 1851 52 17 18 502

to highlight only a subset of the values then

In [106]:
df7.style.background_gradient(cmap = "Blues", subset = ['ka', 'dl', 'mh', 'tn'])
Out[106]:
  an ap ar as br ch ct dl dn ga gj hp hr jh jk ka kl la ld mh ml mn mp mz nl or pb py rj sk tg tn tr up ut wb
date                                                                        
2021-08-10 00:00:00 2 1461 233 929 44 8 112 52 1 141 21 419 23 34 113 1338 21119 7 6 5609 411 633 10 937 102 1041 59 101 11 110 494 1893 205 19 39 639
2021-08-11 00:00:00 0 1869 188 886 47 5 83 37 0 103 16 374 16 14 114 1826 23500 10 5 5560 463 606 10 863 64 1078 105 114 19 157 482 1964 244 24 40 700
2021-08-12 00:00:00 0 1859 180 935 43 12 98 49 1 88 17 354 16 44 176 1857 21445 9 6 6388 429 677 8 576 80 1107 80 109 17 100 453 1942 140 15 24 747
2021-08-13 00:00:00 0 1746 166 763 47 15 77 50 0 67 23 333 26 32 129 1669 20452 6 4 6686 384 522 9 575 81 1193 88 113 24 150 427 1933 180 25 27 739
2021-08-14 00:00:00 0 1535 161 755 39 4 83 50 0 88 25 284 14 28 135 1632 19451 9 7 5787 343 439 16 524 61 1132 50 101 14 129 420 1916 137 42 19 705
2021-08-15 00:00:00 0 1506 48 411 28 1 49 53 0 75 16 182 22 27 97 1431 18582 6 3 4797 420 602 13 467 54 1058 46 79 18 152 245 1896 142 30 16 673
2021-08-16 00:00:00 1 909 165 758 14 2 68 27 2 62 14 276 22 35 77 1065 12294 4 6 4145 352 275 7 249 44 868 30 49 11 20 405 1851 52 17 18 502
In [109]:
df7.style.bar()
Out[109]:
  an ap ar as br ch ct dl dn ga gj hp hr jh jk ka kl la ld mh ml mn mp mz nl or pb py rj sk tg tn tr up ut wb
date                                                                        
2021-08-10 00:00:00 2 1461 233 929 44 8 112 52 1 141 21 419 23 34 113 1338 21119 7 6 5609 411 633 10 937 102 1041 59 101 11 110 494 1893 205 19 39 639
2021-08-11 00:00:00 0 1869 188 886 47 5 83 37 0 103 16 374 16 14 114 1826 23500 10 5 5560 463 606 10 863 64 1078 105 114 19 157 482 1964 244 24 40 700
2021-08-12 00:00:00 0 1859 180 935 43 12 98 49 1 88 17 354 16 44 176 1857 21445 9 6 6388 429 677 8 576 80 1107 80 109 17 100 453 1942 140 15 24 747
2021-08-13 00:00:00 0 1746 166 763 47 15 77 50 0 67 23 333 26 32 129 1669 20452 6 4 6686 384 522 9 575 81 1193 88 113 24 150 427 1933 180 25 27 739
2021-08-14 00:00:00 0 1535 161 755 39 4 83 50 0 88 25 284 14 28 135 1632 19451 9 7 5787 343 439 16 524 61 1132 50 101 14 129 420 1916 137 42 19 705
2021-08-15 00:00:00 0 1506 48 411 28 1 49 53 0 75 16 182 22 27 97 1431 18582 6 3 4797 420 602 13 467 54 1058 46 79 18 152 245 1896 142 30 16 673
2021-08-16 00:00:00 1 909 165 758 14 2 68 27 2 62 14 276 22 35 77 1065 12294 4 6 4145 352 275 7 249 44 868 30 49 11 20 405 1851 52 17 18 502
In [110]:
df7.style.bar(subset=['dl', 'mh', 'tn'])
Out[110]:
  an ap ar as br ch ct dl dn ga gj hp hr jh jk ka kl la ld mh ml mn mp mz nl or pb py rj sk tg tn tr up ut wb
date                                                                        
2021-08-10 00:00:00 2 1461 233 929 44 8 112 52 1 141 21 419 23 34 113 1338 21119 7 6 5609 411 633 10 937 102 1041 59 101 11 110 494 1893 205 19 39 639
2021-08-11 00:00:00 0 1869 188 886 47 5 83 37 0 103 16 374 16 14 114 1826 23500 10 5 5560 463 606 10 863 64 1078 105 114 19 157 482 1964 244 24 40 700
2021-08-12 00:00:00 0 1859 180 935 43 12 98 49 1 88 17 354 16 44 176 1857 21445 9 6 6388 429 677 8 576 80 1107 80 109 17 100 453 1942 140 15 24 747
2021-08-13 00:00:00 0 1746 166 763 47 15 77 50 0 67 23 333 26 32 129 1669 20452 6 4 6686 384 522 9 575 81 1193 88 113 24 150 427 1933 180 25 27 739
2021-08-14 00:00:00 0 1535 161 755 39 4 83 50 0 88 25 284 14 28 135 1632 19451 9 7 5787 343 439 16 524 61 1132 50 101 14 129 420 1916 137 42 19 705
2021-08-15 00:00:00 0 1506 48 411 28 1 49 53 0 75 16 182 22 27 97 1431 18582 6 3 4797 420 602 13 467 54 1058 46 79 18 152 245 1896 142 30 16 673
2021-08-16 00:00:00 1 909 165 758 14 2 68 27 2 62 14 276 22 35 77 1065 12294 4 6 4145 352 275 7 249 44 868 30 49 11 20 405 1851 52 17 18 502

select only a subset of dataset and apply style only to that subset

In [112]:
df7[['mh', 'tn', 'dl']].style.bar()
Out[112]:
  mh tn dl
date      
2021-08-10 00:00:00 5609 1893 52
2021-08-11 00:00:00 5560 1964 37
2021-08-12 00:00:00 6388 1942 49
2021-08-13 00:00:00 6686 1933 50
2021-08-14 00:00:00 5787 1916 50
2021-08-15 00:00:00 4797 1896 53
2021-08-16 00:00:00 4145 1851 27
In [116]:
df7[['mh', 'dl', 'tn']].style.bar(subset=['dl'], color='red').bar(['mh'], color='blue').bar(['tn'], color='green')
Out[116]:
  mh dl tn
date      
2021-08-10 00:00:00 5609 52 1893
2021-08-11 00:00:00 5560 37 1964
2021-08-12 00:00:00 6388 49 1942
2021-08-13 00:00:00 6686 50 1933
2021-08-14 00:00:00 5787 50 1916
2021-08-15 00:00:00 4797 53 1896
2021-08-16 00:00:00 4145 27 1851
In [ ]:
!pip install nbconvert
In [118]:
%shell jupyter nbconvert --to html /content/Padhai_data_tabulation.ipynb
[NbConvertApp] Converting notebook /content/Padhai_data_tabulation.ipynb to html
[NbConvertApp] Writing 3785731 bytes to /content/Padhai_data_tabulation.html
Out[118]:

In [ ]: