Basic Tutorial¶
Introduction¶
In this tutorial you will make a Table Base Table object that contains names of cities and their population. We will then filter the table for cities that have a population greater then a value.
Step 1: Importing and Installing Tablebase¶
The first step to use tablebase is to install it. If you have already installed tablebase skip to step two.
You can install tablebase with pypi (pip). Run the following code in the terminal
pip install tablebase
This code should have an output like this:
Collecting tablebase
Using cached tablebase-0.5.tar.gz (1.5 kB)
Using legacy 'setup.py install' for tablebase, since package 'wheel' is not installed.
Installing collected packages: tablebase
Running setup.py install for tablebase ... done
Successfully installed tablebase-0.5
Great! You have installed tablebase!
Not working? Report a bug.
Lets test that we really installed tablebase. In a python script, execute this line of code.
import tablebase
If this code has no output, then you have installed tablebase successfully!
Step 2: Make a blank table¶
To make a table you must make a tablebase Table object. What is the Table object? The Table object is a blank table with all the commands (filter, new row, ect.).
To make a table object do the following:
import tablebase
My_Table = tablebase.Table()
What did we do in the code above? We imported tablebase. We then set My_Table to a blank table.
Step 3: Add Columns¶
We made a blank table, we now want to add columns. To do this we use the
add_col method. More information can be found here:
tablebase.Table.add_col()
Lets add the following columns: City, Founder, Population
import tablebase
My_Table = tablebase.Table()
My_Table.add_col("City")
My_Table.add_col("Founder")
My_Table.add_col("Population")
Step 4: Adding Rows¶
To add a row we use the add_row method. The add_row method adds a
list to the takes that the user provides. More information can be found here:
tablebase.Table.add_row()
Lets add the following rows: Los Angeles, New York, Plymouth
import tablebase
My_Table = tablebase.Table()
My_Table.add_col("City")
My_Table.add_col("Founder")
My_Table.add_col("Population")
My_Table.add_row(["Los Angeles", "Felipe de Neve", "3967000"])
My_Table.add_row(["New York", "Peter Minuit", "8419000"])
My_Table.add_row(["Plymouth", "William Bradford", "60803"])
Step 5: Print Table to Console¶
We have made our table with all the rows and columns we want. We now just want to see our table.
To do this we will use the table_content variable.
import tablebase
My_Table = tablebase.Table()
My_Table.add_col("City")
My_Table.add_col("Founder")
My_Table.add_col("Population")
My_Table.add_row(["Los Angeles", "Felipe de Neve", "3967000"])
My_Table.add_row(["New York", "Peter Minuit", "8419000"])
My_Table.add_row(["Plymouth", "William Bradford", "60803"])
print(My_Table.table_content)
This code should give a result like this:
[['City', 'Founder', 'Population'], ['Los Angeles', 'Felipe de Neve', '3967000'], ['New York', 'Peter Minuit', '8419000'], ['Plymouth', 'William Bradford', '60803']]
Wait a minute. Is this a table? It might not look like it but it is a list of
lists. If you want to view it in a nicer form, use the display()
method.
import tablebase
My_Table = tablebase.Table()
My_Table.add_col("City")
My_Table.add_col("Founder")
My_Table.add_col("Population")
My_Table.add_row(["Los Angeles", "Felipe de Neve", "3967000"])
My_Table.add_row(["New York", "Peter Minuit", "8419000"])
My_Table.add_row(["Plymouth", "William Bradford", "60803"])
My_Table.display()
City Founder Population
Los Angeles Felipe de Neve 3967000
New York Peter Minuit 8419000
Plymouth William Bradford 60803
This looks more like a table.
Tip
You can use table_content to overwrite the table with a new one.
My_Table.table_content = [["Col1", "Col2"], ["11", "12"], ["21", "22"]]
Step 6: Filter the table¶
We now want to filter the table by population the is greater then 1 million.
We will use the filter method to filter the table and then print out the result.
import tablebase
My_Table = tablebase.Table()
My_Table.add_col("City")
My_Table.add_col("Founder")
My_Table.add_col("Population")
My_Table.add_row(["Los Angeles", "Felipe de Neve", "3967000"])
My_Table.add_row(["New York", "Peter Minuit", "8419000"])
My_Table.add_row(["Plymouth", "William Bradford", "60803"])
My_Table.display()
print(My_Table.filter("int(@Population@) > 1000000").display())
What did we just do? We filtered the Population column in My_Table
with the filter method for cities that have a population greater than
1 million (1000000) people. We had to make Population a integer before
filtering. This returns a Table object with the filtered results. We then
use the display()
method to pretty-print our table.
For more information see
tablebase.Table.filter()
The result should look something like this:
City Founder Population
Los Angeles Felipe de Neve 3967000
New York Peter Minuit 8419000
Plymouth William Bradford 60803
City Founder Population
Los Angeles Felipe de Neve 3967000
New York Peter Minuit 8419000
Notice how the second table does not have Plymouth because it does not have a population greater than 1 million.