qgrid#

qgrid uses SlickGrid to sort, filter and manipulate DataFrames in Jupyter notebooks. In this way you can sort and filter DataFrames and edit the DataFrames by double-clicking on cells.

Installation#

qgrid is installed in both the kernel and the Jupyter environment:

$ pipenv install qgrid

In the Jupyter environment, qgrid must also be activated as notebook extension:

$ pipenv run jupyter nbextension enable --py --sys-prefix qgrid
Enabling notebook extension qgrid/extension...
      - Validating: OK
[1]:
import qgrid

API#

You can find the API documentation at https://qgrid.readthedocs.io/.

Alternatively, you can access the API documentation with the IPython ? Operator:

[2]:
qgrid.show_grid?
[3]:
qgrid.set_defaults?
[4]:
qgrid.set_grid_option?

Examples#

Examples with show_grid and grid_options#

First we get data with get_data_yahoo and render it as usual:

[5]:
import pandas as pd

# Set this pandas option to prevent the grid from being too big
pd.set_option('display.max_rows', 8)

# Get a pandas DataFrame containing the daily prices for the S&P 500 from 1/1/2011 - 1/1/2014
from pandas_datareader.data import get_data_yahoo
spy = get_data_yahoo(
    symbols='SPY',
    start=pd.Timestamp('2011-01-01'),
    end=pd.Timestamp('2014-01-01'),
    adjust_price=True,
)
spy
[5]:
High Low Open Close Volume Adj_Ratio
Date
2011-01-03 102.432390 100.907142 101.717933 101.990875 138725200.0 0.802762
2011-01-04 102.247731 101.300472 102.215620 101.934654 137409700.0 0.802761
2011-01-05 102.528700 101.517219 101.613552 102.464478 133975300.0 0.802762
2011-01-06 102.617026 101.958762 102.504640 102.263809 122519000.0 0.802762
... ... ... ... ... ... ...
2013-12-26 157.229731 156.682727 156.699811 157.144257 63365000.0 0.854695
2013-12-27 157.417734 156.973302 157.349370 157.135696 61814000.0 0.854695
2013-12-30 157.280968 156.904900 157.152755 157.110031 56857000.0 0.854695
2013-12-31 157.853607 157.204031 157.323700 157.853607 86119900.0 0.854695

754 rows × 6 columns

Now we render the DataFrame with qgrid. You can then quickly scroll, sort and filter through hundreds of thousands of lines. If you double click on a cell you can edit it; and the changes change the values stored in the DataFrame.

[6]:
qgrid.show_grid(spy)

While the show_grid function uses a number of optional parameters to configure the behavior of the table, grid_options allows you to pass a dict with the grid options of SlickGrid. In our example below, we’ll use forceFitColumns and defaultColumnWidth to improve qgrid’s ability to handle a large number of columns.

[7]:
qgrid.show_grid(spy, grid_options={'forceFitColumns': False, 'defaultColumnWidth': 200})

Example of a DataFrame with a multi-index#

[8]:
import qgrid
import pandas as pd
from pandas_datareader import wb
df_countries = wb.download(indicator='NY.GDP.PCAP.KD', country=['all'], start=2005, end=2008)
df_countries.columns = ['GDP per capita (constant 2005 US$)']
qgrid.show_grid(df_countries)
/srv/jupyter/.local/share/virtualenvs/python-38-7yDLP4Uh/lib/python3.8/site-packages/qgrid/grid.py:1329: FutureWarning: inplace is deprecated and will be removed in a future version.
  df.index.set_levels(col_series, level=key_index, inplace=True)

Example with interval column#

[9]:
import numpy as np
import pandas as pd
import qgrid

td = np.cumsum(np.random.randint(1, 15*60, 1000))
start = pd.Timestamp('2017-04-17')
df_interval = pd.DataFrame(
    [(start + pd.Timedelta(seconds=d)) for d in td],
    columns=['time'])

freq = '15Min'
start = df_interval['time'].min().floor(freq)
end = df_interval['time'].max().ceil(freq)
bins = pd.date_range(start, end, freq=freq)

df_interval['time_bin'] = pd.cut(df_interval['time'], bins)

qgrid.show_grid(df_interval, show_toolbar=True)

Example with unnamed columns and ipywidgets.Layout#

[10]:
import numpy as np
import pandas as pd
import qgrid
import ipywidgets as ipyw

rng = np.random.default_rng()
df_types = pd.DataFrame(rng.integers(low=1, high=14, size=14))
qgrid_widget = qgrid.show_grid(df_types, show_toolbar=False)
qgrid_widget.layout = ipyw.Layout(width='20%')

qgrid_widget

Example of columns with different data types#

[11]:
import pandas as pd
import qgrid

df = pd.DataFrame({'A': [1.2, 'foo', 4], 'B': [3, 4, 5]})
df = df.set_index(pd.Index(['bar', 7, 3.2]))
view = qgrid.show_grid(df)

view

Example with nan and None#

[12]:
import pandas as pd
import numpy as np
import qgrid

df = pd.DataFrame([(pd.Timestamp('2017-02-02'), None, 3.4), (np.nan, 2, 4.7), (pd.Timestamp('2017-02-03'), 3, None)])

qgrid.show_grid(df)