Styling Tables and DataFrames

Getting started - a simple table

df = pd.DataFrame({
    'text': ['one', 'two', 'three', 'four', 'five'],
    'category': ['odd', 'even', 'odd', 'even', 'odd'],
    'heatmap': [10, 20, 30, 40, 70],
    'bar': [18, 17, 12, 10, 5]
})
df
text category heatmap bar
0 one odd 10 18
1 two even 20 17
2 three odd 30 12
3 four even 40 10
4 five odd 70 5

Using simple default options

adviz.style_table(
    df,
    column_types=['text', 'category', 'heatmap', 'bar'])

Modify look and feel

adviz.style_table(
    df,
    column_types=['text', 'category', 'heatmap', 'bar'],
    column_widths=[0.15, 0.25, 0.2, 0.4],
    title='Styling tables with <b>adviz',
    theme='plotly_dark',
    precision=0,
    width=600,
    height=400,
    font_size=14,
    title_font_size=25)

Multiple columns & multiple types

medals = px.data.medals_long()[['medal', 'nation', 'count']]
medals['medal'] = (medals['medal']
                   .astype('category')
                   .cat.reorder_categories(['bronze', 'silver', 'gold']))
medals = medals.sort_values(['medal', 'count'], ascending=False)
medals
medal nation count
0 gold South Korea 24
1 gold China 10
2 gold Canada 9
4 silver China 15
3 silver South Korea 13
5 silver Canada 12
8 bronze Canada 12
6 bronze South Korea 11
7 bronze China 8

Style the table

adviz.style_table(
    medals,
    column_types=['category', 'category', 'bar'],
    title='Olympic Medals',
    theme='none',
    precision=0,
    width=800,
    height=500)

Using heatmap instead of bar

adviz.style_table(
    medals,
    column_types=['category', 'category', 'heatmap'],
    title='Olympic Medals',
    theme='none',
    precision=0,
    width=800,
    height=400)

Styling many columns with multiple chart types

election = px.data.election().iloc[:, :-1]
election.head()
district Coderre Bergeron Joly total winner result
0 101-Bois-de-Liesse 2481 1829 3024 7334 Joly plurality
1 102-Cap-Saint-Jacques 2525 1163 2675 6363 Joly plurality
2 11-Sault-au-Récollet 3348 2770 2532 8650 Coderre plurality
3 111-Mile-End 1734 4782 2514 9030 Bergeron majority
4 112-DeLorimier 1770 5933 3044 10747 Bergeron majority

Visualizing a more complex table

adviz.style_table(
    election.sort_values('total', ascending=False).head(20),
    column_types=['text', 'heatmap', 'heatmap', 'heatmap', 'bar', 'category', 'category'],
    column_widths=[.3, 0.12, 0.12, 0.12, 0.12, 0.12, 0.12 ],
    precision=0,
    theme='plotly_dark',
    title='Election Results – Top 20',
    height=750,
)

Producing more detailed and readable tables

top5_gdp_percap = (px.data
                   .gapminder()
                   .sort_values(['year', 'gdpPercap'],
                                ascending=[True, False])
                   .groupby('year').head().iloc[:, :-2]
                   [['country', 'continent', 'year', 'gdpPercap', 'lifeExp', 'pop']]
                   .rename(columns={
                       'gdpPercap': 'GDP per capita',
                       'lifeExp': 'Life expectancy',
                       'pop': 'population'}))
top5_gdp_percap.head()
country continent year GDP per capita Life expectancy population
852 Kuwait Asia 1952 108382.35290 55.565 160000
1476 Switzerland Europe 1952 14734.23275 69.620 4815000
1608 United States Americas 1952 13990.48208 68.440 157553000
240 Canada Americas 1952 11367.16112 68.750 14785584
1092 New Zealand Oceania 1952 10556.57566 69.390 1994794

More complex and customized report

adviz.style_table(
    top5_gdp_percap,
    ['text', 'category', 'category', 'bar', 'heatmap', 'heatmap'],
    column_widths=[.1, .07, .05, .12, .12, .12, ],
    theme='seaborn',
    precision=0,
    title="Top 5 countries per year – GDP per capita",
    height=1300)

Visualizing hierarchical data

See also: the adviz.status_codes chart.

status_codes = pd.read_csv('data/status_codes.csv')
status_codes['category'] = status_codes['status'].round(-2)
status_codes
status category
0 200 200
1 200 200
2 200 200
3 200 200
4 200 200
... ... ...
414398 200 200
414399 200 200
414400 200 200
414401 200 200
414402 200 200

414403 rows × 2 columns

Hierarchical table

adviz.style_table(
    status_codes[['category', 'status']].value_counts().reset_index().sort_values(['category', 'count'], ascending=[True, False]),
    column_types=['category', 'text', 'bar'],
    column_widths=[.2, .2, .6],
    precision=0,
    theme='plotly_dark',
    title='Status Codes',
    height=600, width=700)

Get started now:





python3 -m pip install adviz


Explore more advertools data visualizations