Displaying a DataFrame

Pandas is one of the most popular python libraries for data analysis so sooner or later you are bound to use one of their “DataFrames” within your zero-true notebook. Once you have a dataframe that you’d like to display in zero-true, actually getting it to render on the frontend is simple. Take a look at the code snippet below where we load a dataframe from wikipedia and then display it with zero-true (readhtml will require you to have the library lxml installed in the same environment):

import pandas as pd 
import zero_true as zt 

#read dataframe from wikipedia
novels_df = pd.read_html("https://en.wikipedia.org/w/index.php?title=Science_Fiction:_The_100_Best_Novels&oldid=1091082777", match='The 100 Best Novels')[0]

#display dataframe with zero-true
zt.dataframe(id='df',df=novels_df)

Run the cell in your notebook, and watch the dataframe appear below! Like all other zero-true components, dataframes require an ID as well as a pandas df passed as arguments to render. Since dataframes are static components in zero-true there are no values for you to access that users interact with.

Filtering a DataFrame in Zero-True

Let’s take the code above and make it a little bit more exciting by interactively filtering the dataframe with a slider in zero-true:

import pandas as pd 
import zero_true as zt 

novels_df = pd.read_html("https://en.wikipedia.org/w/index.php?title=Science_Fiction:_The_100_Best_Novels&oldid=1091082777", match='The 100 Best Novels')[0]

#define a slider in zero-true to filter the data 
slider=zt.Slider(id='slider',min=1, max=100,label='Move the Slider to Filter the DataFrame')
#set the first value to 100 to keep all the novels in there
if not slider.value:
    slider.value=100

#display the dataframe with zero-true
zt.dataframe(id='df',df=novels_df[0:slider.value])

This will display the “top n books” where n is the value that the user selects using the slider. Dataframes can be useful when you have a range of text and numbers and more standard visualizations like plots fail to capture the entire story.