Skip to main content

Pandas DataFrames and Series as Interactive Tables

Pypi CI codecov.io Language grade: Python Code style: black Notebook Lab Star

Turn pandas DataFrames and Series into interactive datatables in your notebooks with import itables.interactive:

Quick start

Install the package with

pip install itables

Activate the interactive mode for all series and dataframes with

from itables import init_notebook_mode
init_notebook_mode(all_interactive=True)
import world_bank_data as wb

df = wb.get_countries()
df

You don't see any table above? Please either open the HTML export of this notebook, or run this README on Binder!

Or display just one series or dataframe as an interactive table with the show function.

from itables import show

x = wb.get_series("SP.POP.TOTL", mrv=1, simplify_index=True)
show(x)

Supported environments

itables has been tested in the following editors:

  • Jupyter Notebook
  • Jupyter Lab
  • Jupyter nbconvert (i.e. the tables are still interactive in the HTML export of a notebook)
  • Google Colab
  • VS Code (for both Jupyter Notebooks and Python scripts)
  • PyCharm (for Jupyter Notebooks)
  • Nteract

Advanced usage

Row sorting

Select the order in which the row are sorted with the datatables' order argument. By default, the rows are sorted according to the first column (order = [[0, 'asc']]).

If you want to deactivate the sorting, set order = [], either in the show method, or as a global option:

import pandas as pd
import itables.options as opt

opt.order = []  # no sorting
pd.DataFrame({'a':[2,1]})

Pagination

How many rows per page

Select how many entries should appear at once in the table with either the lengthMenu argument of the show function, or with the global option itables.options.lengthMenu:

import itables.options as opt

opt.lengthMenu = [2, 5, 10, 20, 50, 100, 200, 500]
df

Show the table in full

Show the table in full with the paging argument, either in the show method, or in the options:

show(df.head(), paging=False)

Scroll

If you prefer to replace the pagination with a vertical scroll, use for instance

show(df, scrollY="200px", scrollCollapse=True, paging=False)

Table and cell style

Select how your table should look like with the classes argument of the show function, or by changing itables.options.classes. For the list of possible values, see datatables' default style and the style examples.

opt.classes = ["display", "nowrap"]
df
opt.classes = ["display", "cell-border"]
df

Float precision

Floats are rounded using pd.options.display.float_format. Please change that format according to your preference.

import math
import pandas as pd

with pd.option_context("display.float_format", "{:,.2f}".format):
    show(pd.Series([i * math.pi for i in range(1, 6)]))

You may also choose to convert floating numbers to strings:

with pd.option_context("display.float_format", "${:,.2f}".format):
    show(pd.Series([i * math.pi for i in range(1, 6)]))

Advanced cell formatting

Datatables allows to set the cell or row style depending on the cell content, with either the createdRow or createdCell callback. For instance, if we want the cells with negative numbers to be colored in red, we can use the columnDefs.createdCell argument as follows:

show(
    pd.DataFrame([[-1, 2, -3, 4, -5], [6, -7, 8, -9, 10]], columns=list("abcde")),
    columnDefs=[
        {
            "targets": "_all",
            "createdCell": """function (td, cellData, rowData, row, col) {
      if ( cellData < 0 ) {
        $(td).css('color', 'red')
      }
    }""",
        }
    ],
    eval_functions=True,
)

Column width

For tables that are larger than the notebook, the columnDefs argument allows to specify the desired width. If you wish you can also change the default in itables.options.

show(x.to_frame().T, columnDefs=[{"width": "120px", "targets": "_all"}])

Cell alignment

You can use the datatables.net cell classes like dt-left, dt-center, dt-right etc to set the cell alignment. Specify it for one table by using the columnDefs argument of show

show(df, columnDefs=[{"className":"dt-center",  "targets": "_all"}])

or globally by setting opt.columnDefs:

opt.columnDefs = [{"className":"dt-center", "targets": "_all"}]
df
del opt.columnDefs

HTML in cells

import pandas as pd

show(
    pd.Series(
        [
            "<b>bold</b>",
            "<i>italic</i>",
            '<a href="https://github.com/mwouts/itables">link</a>',
        ],
        name="HTML",
    ),
    paging=False,
)

Select rows

Not currently implemented. May be made available at a later stage using the select extension for datatables.

Copy, CSV, PDF and Excel buttons

Not currently implemented. May be made available at a later stage thanks to the buttons extension for datatable.

Downsampling

When the data in a table is larger than maxBytes, which is equal to 64KB by default, itables will display only a subset of the table - one that fits into maxBytes. If you wish, you can deactivate the limit with maxBytes=0, change the value of maxBytes, or similarly set a limit on the number of rows (maxRows, defaults to 0) or columns (maxColumns, defaults to pd.get_option('display.max_columns')).

Note that datatables support server-side processing. At a later stage we may implement support for larger tables using this feature.

df = wb.get_indicators().head(500)
opt.maxBytes = 10000
df.values.nbytes
df

To show the table in full, we can modify the value of maxBytes either locally:

show(df, maxBytes=0)

or globally:

opt.maxBytes = 2**20
df

References

DataTables

  • DataTables is a plug-in for the jQuery Javascript library. It has a great documentation, and a large set of examples.
  • The R package DT uses datatables.net as the underlying library for both R notebooks and Shiny applications. In addition to the standard functionalities of the library (display, sort, filtering and row selection), RStudio seems to have implemented cell edition.

Alternatives

ITables uses basic Javascript. It is not a Jupyter widget, which means that it does not allows you to edit the content of the dataframe.

If you are looking for Jupyter widgets, have a look at

If you are looking for a table component that will fit in Dash applications, see datatable by Dash.

Please also checkout D-Tale for exploring your Python DataFrames in the browser, using a local server.

<script async defer src="https://buttons.github.io/buttons.js"></script>

Metadata

Release files for itables 0.4.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for itables 0.4.1
File Size Uploaded
itables-0.4.1.tar.gz 14.0 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for itables 0.4.1
File Interpreter ABI Platform
itables-0.4.1-py3-none-any.whl Python 3 none any Details

Total release size: 27.1 kB

Release files / itables-0.4.1.tar.gz

Download URL itables-0.4.1.tar.gz
Size 14.0 kB
Tags Source
SHA-256 checksum
How to use checksums
267d7e3ab6ac36f207e864d63ea5724ea94b242d91a5a010005d5f457d5305a1
BLAKE2b-256 checksum
How to use checksums
99fe0e87be1aad6b01e9e3f7cd63bdafce99e228e73fe3ef7115cc9689c7d414
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.7.1 importlib_metadata/4.10.0 pkginfo/1.8.2 requests/2.27.1 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.7.12

Release files / itables-0.4.1-py3-none-any.whl

Download URL itables-0.4.1-py3-none-any.whl
Size 13.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
a732085495d7922a7dec614410c87eebb3ad5bdb415d4fcfbe0d52a84e385403
BLAKE2b-256 checksum
How to use checksums
3020388e374e96ccf7a2b5fcae0e4885a03c4ca330b83657a789975a47882527
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.7.1 importlib_metadata/4.10.0 pkginfo/1.8.2 requests/2.27.1 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.7.12

Release history Release notifications | RSS feed

2.9.1

2 release files

2.9.0

2 release files

2.8.2

2 release files

2.8.1

2 release files

2.8.0

2 release files

2.7.3

2 release files

2.7.2

2 release files

2.7.1

2 release files

2.7.0

2 release files

2.6.2

2 release files

2.6.1

2 release files

2.6.0

2 release files

2.5.2

2 release files

2.5.1

2 release files

2.5.0

2 release files

2.4.5

2 release files

2.4.4

2 release files

2.4.3

2 release files

2.4.2

2 release files

2.4.1

2 release files

2.4.0

2 release files

2.3.0

2 release files

2.2.5

2 release files

2.2.4

2 release files

2.2.3

2 release files

2.2.2

2 release files

2.2.1

2 release files

2.2.0

2 release files

2.1.5

2 release files

2.1.4

2 release files

2.1.3

2 release files

2.1.2

2 release files

2.1.1

2 release files

2.1.0

2 release files

2.0.1

2 release files

2.0.0

2 release files

1.7.1

2 release files

1.7.0

2 release files

1.6.4

2 release files

1.6.3

2 release files

1.6.2

2 release files

1.6.1

2 release files

1.6.0

2 release files

1.5.4

2 release files

1.5.3

2 release files

1.5.2

2 release files

1.5.1

2 release files

1.5.0

2 release files

1.4.6

2 release files

1.4.5

2 release files

1.4.4

2 release files

1.4.3

2 release files

1.4.2

2 release files

1.4.1

2 release files

1.4.0

2 release files

1.3.5

2 release files

1.3.4

2 release files

1.3.3

2 release files

1.3.2

2 release files

1.3.1

2 release files

1.3.0

2 release files

1.2.0

2 release files

1.1.3

2 release files

1.1.2

2 release files

1.1.1

2 release files

1.1.0

2 release files

1.0.0

2 release files

0.4.7

2 release files

0.4.6

2 release files

0.4.5

2 release files

0.4.4

2 release files

0.4.3

2 release files

0.4.2

2 release files

This release

0.4.1 This release

2 release files

0.4.0

2 release files

0.3.1

2 release files

0.3.0

2 release files

0.2.2

1 release file

0.2.1

1 release file

0.2.0

1 release file

0.1.0

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page