bamboolib
Preview
This feature is in Public Preview.
Note
bamboolib is supported in Databricks Runtime 11.3 LTS and above.
bamboolib is a user interface component that allows no-code data analysis and transformations from within a Databricks notebook. bamboolib helps users more easily work with their data and speeds up common data wrangling, exploration, and visualization tasks. As users complete these kinds of tasks with their data, bamboolib automatically generates Python code in the background. Users can share this code with others, who can run this code in their own notebooks to quickly reproduce those original tasks. They can also use bamboolib to extend those original tasks with additional data tasks, all without needing to know how to code. Those who are experienced with coding can extend this code to create even more sophisticated results.
Behind the scenes, bamboolib uses ipywidgets, which is an interactive HTML widget framework for the IPython kernel. ipywidgets runs inside of the IPython kernel.
Requirements
A Databricks notebook, which is attached to a Databricks cluster with Databricks Runtime 11.0 or above.
The
bamboolib
library must be available to the notebook.To install the library from PyPI only on a specific cluster, see Cluster libraries.
To use the
%pip
command to make the library available only to a specific notebook, see Notebook-scoped Python libraries.
Quickstart
Create a Python notebook.
Attach the notebook to a cluster that meets the requirements.
In the notebook’s first cell, enter the following code, and then run the cell. This step can be skipped if bamboolib is already installed in the workspace or cluster.
%pip install bamboolib
In the notebook’s second cell, enter the following code, and then run the cell.
import bamboolib as bam
In the notebook’s third cell, enter the following code, and then run the cell.
bam
Note
Alternatively, you can print an existing pandas DataFrame to display bamboolib for use with that specific DataFrame.
Continue with key tasks.
Walkthroughs
You can use bamboolib by itself or with an existing pandas DataFrame.
Use bamboolib by itself
In this walkthrough, you use bamboolib to display in your notebook the contents of an example sales data set. You then experiment with some of the related notebook code that bamboolib automatically generates for you. You finish by querying and sorting a copy of the sales data set’s contents.
Create a Python notebook.
Attach the notebook to a cluster that meets the requirements.
In the notebook’s first cell, enter the following code, and then run the cell. This step can be skipped if bamboolib is already installed in the workspace or cluster.
%pip install bamboolib
In the notebook’s second cell, enter the following code, and then run the cell.
import bamboolib as bam
In the notebook’s third cell, enter the following code, and then run the cell.
bam
Click Load dummy data.
In the Load dummy data pane, for Load a dummy data set for testing bamboolib, select Sales dataset.
Click Execute.
Display all of the rows where item_type is Baby Food:
In the Search actions list, select Filter rows.
In the Filter rows pane, in the Choose list (above where), select Select rows.
In the list below where, select item_type.
In the Choose list next to item_type, select has value(s).
In the Choose value(s) box next to has value(s), select Baby Food.
Click Execute.
Copy the automatically generated Python code for this query:
Cick Copy Code below the data preview.
Paste and modify the code:
In the notebook’s fourth cell, paste the code that you copied. It should look like this:
import pandas as pd df = pd.read_csv(bam.sales_csv) # Step: Keep rows where item_type is one of: Baby Food df = df.loc[df['item_type'].isin(['Baby Food'])]
Add to this code so that it displays only those rows where order_prio is C, and then run the cell:
import pandas as pd df = pd.read_csv(bam.sales_csv) # Step: Keep rows where item_type is one of: Baby Food df = df.loc[df['item_type'].isin(['Baby Food'])] # Add the following code. # Step: Keep rows where order_prio is one of: C df = df.loc[df['order_prio'].isin(['C'])] df
Tip
Instead of writing this code, you can also do the same thing by just using bamboolib in the third cell to display only those rows where order_prio is C. This step is an example of extending the code that bamboolib automatically generated earlier.
Sort the rows by region in ascending order:
In the widget within the fourth cell, in the Search actions list, select Sort rows.
In the Sort column(s) pane, in the Choose column list, select region.
In the list next to region, select ascending (A-Z).
Click Execute.
Note
This is equivalent to writing the following code yourself:
df = df.sort_values(by=['region'], ascending=[True]) df
You could have also just used bamboolib in the third cell to sort the rows by region in ascending order. This step demonstrates how you can use bamboolib to extend the code that you write. As you use bamboolib, it automatically generates the additional code for you in the background, so that you can further extend your already-extended code!
Continue with key tasks.
Use bamboolib with an existing DataFrame
In this walkthrough, you use bamboolib to display in your notebook the contents of a pandas DataFrame. This DataFrame contains a copy of an example sales data set. You then experiment with some of the related notebook code that bamboolib automatically generates for you. You finish by querying and sorting some of the DataFrame’s contents.
Create a Python notebook.
Attach the notebook to a cluster that meets the requirements.
In the notebook’s first cell, enter the following code, and then run the cell. This step can be skipped if bamboolib is already installed in the workspace or cluster.
%pip install bamboolib
In the notebook’s second cell, enter the following code, and then run the cell.
import bamboolib as bam
In the notebook’s third cell, enter the following code, and then run the cell.
import pandas as pd df = pd.read_csv(bam.sales_csv) df
Note that bamboolib only supports pandas DataFrames. To convert a PySpark DataFrame to a pandas DataFrame, call toPandas on the PySpark DataFrame. To convert a Pandas API on Spark DataFrame to a pandas DataFrame, call to_pandas on the Pandas API on Spark DataFrame.
Click Show bamboolib UI.
Display all of the rows where item_type is Baby Food:
In the Search actions list, select Filter rows.
In the Filter rows pane, in the Choose list (above where), select Select rows.
In the list below where, select item_type.
In the Choose list next to item_type, select has value(s).
In the Choose value(s) box next to has value(s), select Baby Food.
Click Execute.
Copy the automatically generated Python code for this query. To do this, click Copy Code below the data preview.
Paste and modify the code:
In the notebook’s fourth cell, paste the code that you copied. It should look like this:
# Step: Keep rows where item_type is one of: Baby Food df = df.loc[df['item_type'].isin(['Baby Food'])]
Add to this code so that it displays only those rows where order_prio is C, and then run the cell:
# Step: Keep rows where item_type is one of: Baby Food df = df.loc[df['item_type'].isin(['Baby Food'])] # Add the following code. # Step: Keep rows where order_prio is one of: C df = df.loc[df['order_prio'].isin(['C'])] df
Tip
Instead of writing this code, you can also do the same thing by just using bamboolib in the third cell to display only those rows where order_prio is C. This step is an example of extending the code that bamboolib automatically generated earlier.
Sort the rows by region in ascending order:
a. In the widget within the fourth cell, click Sort rows.
In the Sort column(s) pane, in the Choose column list, select region.
In the list next to region, select ascending (A-Z).
Click Execute.
Note
This is equivalent to writing the following code yourself:
df = df.sort_values(by=['region'], ascending=[True]) df
You could have also just used bamboolib in the third cell to sort the rows by region in ascending order. This step demonstrates how you can use bamboolib to extend the code that you write. As you use bamboolib, it automatically generates the additional code for you in the background, so that you can further extend your already-extended code!
Continue with key tasks.
Key tasks
In this section:
Add the widget to a cell
Scenario: You want the bamboolib widget to display in a cell.
Make sure the notebook meets the requirements for bamboolib.
If bamboolib is not already installed in the workspace or cluster run the following code in a cell in the notebook, preferably in the first cell:
%pip install bamboolib
Run the following code in the notebook, preferably in the notebook’s first or second cell:
import bamboolib as bam
Option 1: In the cell where you want the widget to appear, add the following code, and then run the cell:
bam
The widget appears in the cell below the code.
Or:
Option 2: In a cell that contains a reference to a pandas DataFrame, print the DataFrame. For example, given the following DataFrame definition, run the cell:
import pandas as pd from datetime import datetime, date df = pd.DataFrame({ 'a': [ 1, 2, 3 ], 'b': [ 2., 3., 4. ], 'c': [ 'string1', 'string2', 'string3' ], 'd': [ date(2000, 1, 1), date(2000, 2, 1), date(2000, 3, 1) ], 'e': [ datetime(2000, 1, 1, 12, 0), datetime(2000, 1, 2, 12, 0), datetime(2000, 1, 3, 12, 0) ] }) df
The widget appears in the cell below the code.
Note that bamboolib only supports pandas DataFrames. To convert a PySpark DataFrame to a pandas DataFrame, call toPandas on the PySpark DataFrame. To convert a Pandas API on Spark DataFrame to a pandas DataFrame, call to_pandas on the Pandas API on Spark DataFrame.
Clear the widget
Scenario: You want to clear the contents of a widget and then read new data into the existing widget.
Option 1: Run the following code within the cell that contains the target widget:
bam
The widget clears and then redisplays the Databricks: Read CSV file from DBFS, Databricks: Load database table, and Load dummy data buttons.
Note
If the error name 'bam' is not defined
appears, run the following code in the notebook (preferably in the notebook’s first cell), and then try again:
import bamboolib as bam
Option 2: In a cell that contains a reference to a pandas DataFrame, print the DataFrame again by running the cell again. The widget clears and then displays the new data.
Data loading tasks
In this section:
Read an example dataset’s contents into the widget
Scenario: You want to read some example data into the widget, for example some pretend sales data, so that you can test out the widget’s functionality.
Click Load dummy data.
Note
If Load dummy data is not visible, clear the widget with Option 1 and try again.
In the Load dummy data pane, for Load a dummy data set for testing bamboolib, select the name of the dataset that you want to load.
For Dataframe name, enter a name for the programmatic identifier of the table’s contents as a DataFrame, or leave df as the default programmatic identifier.
Click Execute.
The widget displays the contents of the dataset.
Tip
You can switch the current widget to display the contents of a different example dataset:
In the current widget, click the Load dummy data tab.
Follow the preceding steps to read the other example dataset’s contents into the widget.
Read a CSV file’s contents into the widget
Scenario: You want to read the contents of a CSV file within your Databricks workspace into the widget.
Click Databricks: Read CSV file from DBFS.
Note
If Databricks: Read CSV file from DBFS is not visible, clear the widget with Option 1 and try again.
In the Read CSV from DBFS pane, browse to the location that contains the target CSV file.
Select the target CSV file.
For Dataframe name, enter a name for the programmatic identifier of the CSV file’s contents as a DataFrame, or leave df as the default programmatic identifier.
For CSV value separator, enter the character that separates values in the CSV file, or leave the , (comma) character as the default value separator.
For Decimal separator, enter the character that separates decimals in the CSV file, or leave the . (dot) character as the default value separator.
For Row limit: read the first N rows - leave empty for no limit, enter the maximum number of rows to read into the widget, or leave 100000 as the default number of rows, or leave this box empty to specify no row limit.
Click Open CSV file.
The widget displays the contents of the CSV file, based on the settings that you specified.
Tip
You can switch the current widget to display the contents of a different CSV file:
In the current widget, click the Read CSV from DBFS tab.
Follow the preceding steps to read the other CSV file’s contents into the widget.
Read a database table’s contents into the widget
Scenario: You want to read the contents of a database table within your Databricks workspace into the widget.
Click Databricks: Load database table.
Note
If Databricks: Load database table is not visible, clear the widget with Option 1 and try again.
In the Databricks: Load database table pane, for Database - leave empty for default database, enter the name of the database in which the target table is located, or leave this box empty to specify the default database.
For Table, enter the name of the target table.
For Row limit: read the first N rows - leave empty for no limit, enter the maximum number of rows to read into the widget, or leave 100000 as the default number of rows, or leave this box empty to specify no row limit.
For Dataframe name, enter a name for the programmatic identifier of the table’s contents as a DataFrame, or leave df as the default programmatic identifier.
Click Execute.
The widget displays the contents of the table, based on the settings that you specified.
Tip
You can switch the current widget to display the contents of a different table:
In the current widget, click the Databricks: Load database table tab.
Follow the preceding steps to read the other table’s contents into the widget.
Data action tasks
bamboolib offers over 50 data actions. Following are some of the more common getting-started data action tasks.
In this section:
Select columns
Scenario: You want to show only specific table columns by name, by data type, or that match some regular expression. For example, in the dummy Sales dataset, you want to show only the item_type
and sales_channel
columns, or you want to show only the columns that contain the string _date
in their column names.
On the Data tab, in the Search actions drop-down list, do one of the following:
Type select, and then select Select or drop columns.
Select Select or drop columns.
In the Select or drop columns pane, in the Choose drop-down list, select Select.
Select the target column names or inclusion criterion.
For Dataframe name, enter a name for the programmatic identifier of the table’s contents as a DataFrame, or leave df as the default programmatic identifier.
Click Execute.
Drop columns
Scenario: You want to hide specific table columns by name, by data type, or that match some regular expression. For example, in the dummy Sales dataset, you want to hide the order_prio
, order_date
, and ship_date
columns, or you want to hide all columns that contain only date-time values.
On the Data tab, in the Search actions drop-down list, do one of the following:
Type drop, and then select Select or drop columns.
Select Select or drop columns.
In the Select or drop columns pane, in the Choose drop-down list, select Drop.
Select the target column names or inclusion criterion.
For Dataframe name, enter a name for the programmatic identifier of the table’s contents as a DataFrame, or leave df as the default programmatic identifier.
Click Execute.
Filter rows
Scenario: You want to show or hide specific table rows based on criteria such as specific column values that are matching or missing. For example, in the dummy Sales dataset, you want to show only those rows where the item_type
column’s value is set to Baby Food
.
On the Data tab, in the Search actions drop-down list, do one of the following:
Type filter, and then select Filter rows.
Select Filter rows.
In the Filter rows pane, in the Choose drop-down list above where, select Select rows or Drop rows.
Specify the first filter criterion.
To add another filter criterion, click add condition, and specify the next filter criterion. Repeat as desired.
For Dataframe name, enter a name for the programmatic identifier of the table’s contents as a DataFrame, or leave df as the default programmatic identifier.
Click Execute.
Sort rows
Scenario: You want to sort table rows based on the values within one or more columns. For example, in the dummy Sales dataset, you want to show the rows by the region
column’s values in alphabetical order from A to Z.
On the Data tab, in the Search actions drop-down list, do one of the following:
Type sort, and then select Sort rows.
Select Sort rows.
In the Sort column(s) pane, choose the first column to sort by and the sort order.
To add another sort criterion, click add column, and specify the next sort criterion. Repeat as desired.
For Dataframe name, enter a name for the programmatic identifier of the table’s contents as a DataFrame, or leave df as the default programmatic identifier.
Click Execute.
Grouping rows and columns tasks
In this section:
Group rows and columns by a single aggregate function
Scenario: You want to show row and column results by calculated groupings, and you want to assign custom names to those groupings. For example, in the dummy Sales dataset, you want to group the rows by the country
column’s values, showing the numbers of rows containing the same country
value, and giving the list of calculated counts the name country_count
.
On the Data tab, in the Search actions drop-down list, do one of the following:
Type group, and then select Group by and aggregate (with renaming).
Select Group by and aggregate (with renaming).
In the Group by with column rename pane, select the columns to group by, the first calculation, and optionally specify a name for the calculated column.
To add another calculation, click add calculation, and specify the next calculation and column name. Repeat as desired.
Specify where to store the result.
For Dataframe name, enter a name for the programmatic identifier of the table’s contents as a DataFrame, or leave df as the default programmatic identifier.
Click Execute.
Group rows and columns by multiple aggregate functions
Scenario: You want to show row and column results by calculated groupings. For example, in the dummy Sales dataset, you want to group the rows by the region
, country
, and sales_channel
columns’ values, showing the numbers of rows containing the same region
and country
value by sales_channel
, as well as the total_revenue
by unique combination of region
, country
, and sales_channel
.
On the Data tab, in the Search actions drop-down list, do one of the following:
Type group, and then select Group by and aggregate (default).
Select Group by and aggregate (default).
In the Group by with column rename pane, select the columns to group by and the first calculation.
To add another calculation, click add calculation, and specify the next calculation. Repeat as desired.
Specify where to store the result.
For Dataframe name, enter a name for the programmatic identifier of the table’s contents as a DataFrame, or leave df as the default programmatic identifier.
Click Execute.
Remove rows with missing values
Scenario: You want to remove any row that has a missing value for the specified columns. For example, in the dummy Sales dataset, you want to remove any rows that have a missing item_type
value.
On the Data tab, in the Search actions drop-down list, do one of the following:
Type drop or remove, and then select Drop missing values.
Select Drop missing values.
In the Drop missing values pane, select the columns to remove any row that has a missing value for that column.
For Dataframe name, enter a name for the programmatic identifier of the table’s contents as a DataFrame, or leave df as the default programmatic identifier.
Click Execute.
Remove duplicated rows
Scenario: You want to to remove any row that has a duplicated value for the specified columns. For example, in the dummy Sales dataset, you want to remove any rows that are exact duplicates of each other.
On the Data tab, in the Search actions drop-down list, do one of the following:
Type drop or remove, and then select Drop/Remove duplicates.
Select Drop/Remove duplicates.
In the Remove Duplicates pane, select the columns to remove any row that has a duplicated value for those columns, and then select whether to keep the first or last row that has the duplicated value.
For Dataframe name, enter a name for the programmatic identifier of the table’s contents as a DataFrame, or leave df as the default programmatic identifier.
Click Execute.
Find and replace missing values
Scenario: You want to replace the missing value with a replacement value for any row with the specified columns. For example, in the dummy Sales dataset, you want to replace any row with a missing value in the item_type
column with the value Unknown Item Type
.
On the Data tab, in the Search actions drop-down list, do one of the following:
Type find or replace, and then select Find and replace missing values.
Select Find and replace missing values.
In the Replace missing values pane, select the columns to replace missing values for, and then specify the replacement value.
Click Execute.
Create a column formula
Scenario: You want to create a column that uses a unique formula. For example, in the dummy Sales dataset, you want to create a column named profit_per_unit
that displays the result of dividing the total_profit
column value by the units_sold
column value for each row.
On the Data tab, in the Search actions drop-down list, do one of the following:
Type formula, and then select New column formula.
Select New column formula.
In the Replace missing values pane, select the columns to replace missing values for, and then specify the replacement value.
Click Execute.
Data action history tasks
In this section:
View the list of actions taken in the widget
Scenario: You want to see a list of all of the changes that were made in the widget, starting with the most recent change.
Click History. The list of actions appears in the Transformations history pane.
Undo the most recent action taken in the widget
Scenario: You want to revert the most recent change that was made in the widget.
Do one of the following:
Click the counterclockwise arrow icon.
Click History, and in the Transformations history pane, click Undo last step.
Redo the most recent action taken in the widget
Scenario: You want to revert the most recent revert that was made in the widget.
Do one of the following:
Click the clockwise arrow icon.
Click History, and in the Transformations history pane, click Recover last step.
Change the most recent action taken in the widget
Scenario: You want to change the most recent change that was taken in the widget.
Do one of the following:
Click the pencil icon.
Click History, and in the Transformations history pane, click Edit last step.
Make the desired change, and then click Execute.
Get code to programmatically recreate the widget’s current state as a DataFrame
Scenario: You want to get Python code that programmatically recreates the current widget’s state, represented as a pandas DataFrame. You want to run this code in a different cell in this workbook or a different workbook altogether.
Click Get Code.
In the Export code pane, click Copy code. The code is copied to your system’s clipboard.
Paste the code into a different cell in this workbook or into a different workbook.
Write additional code to work with this pandas DataFrame programmatically, and then run the cell. For example, to display the DataFrame’s contents, assuming that your DataFrame is represented programmatically by
df
:# Your pasted code here, followed by... df
Limitations
See Known limitations Databricks notebooks for more more information.