<jupyter_start><jupyter_text>Tables are a fundamental object type for representing data sets. A table can be viewed in two ways:* a sequence of named columns that each describe a single aspect of all entries in a data set, or* a sequence of rows that each contain all information about a single entry in a data set.In order to use tables, import all of the module called `datascience`, a module created for this text.<jupyter_code>from datascience import *<jupyter_output><empty_output><jupyter_text>Empty tables can be created using the `Table` function. An empty table is usefuly because it can be extended to contain new rows and columns.<jupyter_code>Table()<jupyter_output><empty_output><jupyter_text>The `with_columns` method on a table constructs a new table with additional labeled columns. Each column of a table is an array. To add one new column to a table, call `with_columns` with a label and an array. (The `with_column` method can be used with the same effect.)Below, we begin each example with an empty table that has no columns.<jupyter_code>Table().with_columns('Number of petals', make_array(8, 34, 5))<jupyter_output><empty_output><jupyter_text>To add two (or more) new columns, provide the label and array for each column. All columns must have the same length, or an error will occur.<jupyter_code>Table().with_columns(
    'Number of petals', make_array(8, 34, 5),
    'Name', make_array('lotus', 'sunflower', 'rose')
)<jupyter_output><empty_output><jupyter_text>We can give this table a name, and then extend the table with another column.<jupyter_code>flowers = Table().with_columns(
    'Number of petals', make_array(8, 34, 5),
    'Name', make_array('lotus', 'sunflower', 'rose')
)

flowers.with_columns(
    'Color', make_array('pink', 'yellow', 'red')
)<jupyter_output><empty_output><jupyter_text>The `with_columns` method creates a new table each time it is called, so the original table is not affected. For example, the table `flowers` still has only the two columns that it had when it was created.<jupyter_code>flowers<jupyter_output><empty_output><jupyter_text>Creating tables in this way involves a lot of typing. If the data have already been entered somewhere, it is usually possible to use Python to read it into a table, instead of typing it all in cell by cell.Often, tables are created from files that contain comma-separated values. Such files are called CSV files.Below, we use the Table method `read_table` to read a CSV file that contains some of the data used by Minard in his graphic about Napoleon's Russian campaign. The data are placed in a table named `minard`.<jupyter_code>minard = Table.read_table(path_data + 'minard.csv')
minard<jupyter_output><empty_output><jupyter_text>We will use this small table to demonstrate some useful Table methods. We will then use those same methods, and develop other methods, on much larger tables of data.  The Size of the Table The method `num_columns` gives the number of columns in the table, and `num_rows` the number of rows.<jupyter_code>minard.num_columns
minard.num_rows<jupyter_output><empty_output><jupyter_text>Column Labels The method `labels` can be used to list the labels of all the columns. With `minard` we don't gain much by this, but it can be very useful for tables that are so large that not all columns are visible on the screen.<jupyter_code>minard.labels<jupyter_output><empty_output><jupyter_text>We can change column labels using the `relabeled` method. This creates a new table and leaves `minard` unchanged.<jupyter_code>minard.relabeled('City', 'City Name')<jupyter_output><empty_output><jupyter_text>However, this method does not change the original table.<jupyter_code>minard<jupyter_output><empty_output><jupyter_text>A common pattern is to assign the original name `minard` to the new table, so that all future uses of `minard` will refer to the relabeled table.<jupyter_code>minard = minard.relabeled('City', 'City Name')
minard<jupyter_output><empty_output><jupyter_text>Accessing the Data in a Column We can use a column's label to access the array of data in the column.<jupyter_code>minard.column('Survivors')<jupyter_output><empty_output><jupyter_text>The 5 columns are indexed 0, 1, 2, 3, and 4. The column `Survivors` can also be accessed by using its column index.<jupyter_code>minard.column(4)<jupyter_output><empty_output><jupyter_text>The 8 items in the array are indexed 0, 1, 2, and so on, up to 7. The items in the column can be accessed using `item`, as with any array.<jupyter_code>minard.column(4).item(0)
minard.column(4).item(5)<jupyter_output><empty_output><jupyter_text>Working with the Data in a Column Because columns are arrays, we can use array operations on them to discover new information. For example, we can create a new column that contains the percent of all survivors at each city after Smolensk.<jupyter_code>initial = minard.column('Survivors').item(0)
minard = minard.with_columns(
    'Percent Surviving', minard.column('Survivors')/initial
)
minard<jupyter_output><empty_output><jupyter_text>To make the proportions in the new columns appear as percents, we can use the method `set_format` with the option `PercentFormatter`. The `set_format` method takes `Formatter` objects, which exist for dates (`DateFormatter`), currencies (`CurrencyFormatter`), numbers, and percentages.<jupyter_code>minard.set_format('Percent Surviving', PercentFormatter)<jupyter_output><empty_output><jupyter_text>Choosing Sets of Columns The method `select` creates a new table that contains only the specified columns.<jupyter_code>minard.select('Longitude', 'Latitude')<jupyter_output><empty_output><jupyter_text>The same selection can be made using column indices instead of labels.<jupyter_code>minard.select(0, 1)<jupyter_output><empty_output><jupyter_text>The result of using `select` is a new table, even when you select just one column.<jupyter_code>minard.select('Survivors')<jupyter_output><empty_output><jupyter_text>Notice that the result is a table, unlike the result of `column`, which is an array.<jupyter_code>minard.column('Survivors')<jupyter_output><empty_output><jupyter_text>Another way to create a new table consisting of a set of columns is to `drop` the columns you don't want.<jupyter_code>minard.drop('Longitude', 'Latitude', 'Direction')<jupyter_output><empty_output><jupyter_text>Neither `select` nor `drop` change the original table. Instead, they create new smaller tables that share the same data. The fact that the original table is preserved is useful! You can generate multiple different tables that only consider certain columns without worrying that one analysis will affect the other.<jupyter_code>minard<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.08120995380718224, 'fraction_numerical': 0.004619281776188347, 'mean_word_length': 6.383938393839384, 'pattern_counts': {'":': 0, '<': 93, '<?xml version=': 0, '>': 93, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '23', 'dir': 'jupyter-structured-clean-dedup', 'id': '434704', 'license': 'MIT', 'n_tokens_mistral': 2014, 'n_tokens_neox': 1954, 'n_words': 799, 'path': 'notebooks/06/Tables.ipynb', 'repo_name': 'choldgraf/textbook-jekyll', 'size': '6711'}