# Python Reference

### String Methods

Name Chapter Description
`str.split(separator)` N/A Splits the string `str` into a list based on the string `separator` that is passed in
`str.join(array)` N/A Combines each element of `array` into one string, with string `str` being placed in between each element of `array`
`str.replace(old_string, new_string)` Ch 4.2 Replaces each occurrence of `old_string` in `str` with the value of `new_string`

### Array Functions and Methods

Name Chapter Description
`max(array)` Ch 3.3 Returns the maximum value of an array
`min(array)` Ch 3.3 Returns the minimum value of an array
`sum(array)` Ch 3.3 Returns the sum of the values in an array
`abs(num)`
`np.abs(array)`
Ch 3.3 Take the absolute value of number or each number in an array
`round(num)`
`np.round(array)`
Ch 3.3 Round number or array of numbers to the nearest integer
`len(array)` Ch 3.3 Returns the length (number of elements) of an array
`make_array(val1, val2, ...)` Ch 5 Makes a numpy array with the values passed in
`np.average(array)`
`np.mean(array)`
Ch 5.1 Returns the mean value of an array
`np.diff(array)` Ch 5.1 Returns a new array of size `len(arr)-1` with elements equal to the difference between adjacent elements; `val_2 - val_1`, `val_3 - val_2`, etc.
`np.sqrt(array)` Ch 5.1 Returns an array with the square root of each element
`np.arange(stop)`
`np.arange(start, stop)`
`np.arange(start, stop, step)`
Ch 5.2 An array of numbers starting with `start`, going up in increments of `step`, and going up to but not including `stop`. When `start` and/or `step` are left out, default values are used in their place. The default for `step` is 1 and the default for `start` is 0.
`array.item(index)` Ch 5.3 Returns the i-th item in an array (remember Python indices start at 0!)

### Table Functions and Methods

In the examples in the left column, `np` refers to the `NumPy` module, as usual. Everything else is a function, a method, an example of an argument to a function or method, or an example of an object we might call the method on. For example, `tbl` refers to a table, `array` refers to an array, and `num` refers to a number. `array.item(0)` is an example call for the method item, and in that example, `array` is the name previously given to some array.

Name Chapter Description Input Output
`Table()` Ch 6 Create an empty table, usually to extend with data None
`Table().read_table(filename)` Ch 6 Create a table from a data file `filename` (string): the name of the file `Table` with the contents of the data file
`tbl.with_columns(name, values)`
`tbl.with_columns(n1, v1, n2, v2,...)`
Ch 6 Create a table with an additional or replaced column or columns `name` (string): the name of the new column
`array`: the values in that column
`Table`: a copy of the original Table with the new columns added
`tbl.column(column_name_or_index)` Ch 6 Give the values of a column as an array `column_name_or_index` (string or int): the column name or index `array`: the values in the specified column
`tbl.num_rows` Ch 6 Compute the number of rows in a table None `int`: the number of rows in the table
`tbl.num_columns` Ch 6 Compute the number of columns in a table None `int`: the number of columns in the table
`tbl.labels` Ch 6 List the column labels in a table None `array`: the names of each column (as strings) in the table
`tbl.select(col1, col2, ...)` Ch 6 Create a copy of a table with only some of the columns where each column is the column name or index `col` (string or int): column name(s) or index(es) `Table` with the selected columns
`tbl.drop(col1, col2, ...)` Ch 6 Create a copy of a table without some of the columns where each column is the column name or index `col` (string or int}: column name(s) or index(es) `Table` without the selected columns
`tbl.relabel(old_label, new_label)` Ch 6 Modify the existing table in place, changing the column heading in the first argument to the second `old_label` (string) the old column name
`new_label` (string) the new column name
`Table`: a copy of the original with the changed label
`tbl.show(n)` Ch 6.1 Display `n` rows of a table; if no argument is specified, defaults to displaying the entire table `n` (int, optional): number of rows to display None: displays a table with `n` rows
`tbl.sort(column_name_or_index)` Ch 6.1 Create a copy of a table sorted by the values in a column; defaults to ascending order unless `descending = True` is included `column_name_or_index` (string or int): the column name or index
`descending = True` (optional): sort in descending order
`Table`: a copy of the original with the column sorted
`tbl.where(column, predicate)` Ch 6.2 Create a copy of a table with only the rows that match some predicate (see `Table.where` predicates below) `column` (string or int): column name or index
`predicate`: a `Table.where` predicate
`Table`: a copy of the original table with only the rows that match the predicate
`tbl.take(row_indices)` Ch 6.2 A table with only the rows at the given indices `row_indices` (array or int): an array indices of the rows to be included in the Table or in the case of an int, the for index of the row to be included `Table`: a copy of the original table with only the rows at the given indices
`tbl.scatter(x_column, y_column)` Ch 7 Draws a scatter plot consisting of one point for each row of the table; note that `x_column` and `y_column` must be strings specifying column names `x_column` (string): name of the column on the x-axis
`y_column` (string): name of the column on the y-axis
None: draws a scatter plot
`tbl.plot(x_column, y_column)` Ch 7 Draw a line graph consisting of one point for each row of the table `x_column` (string): name of the column on the x-axis
`y_column` (string): name of the column on the y-axis
None: draws a line graph
`tbl.barh(categories)`
`tbl.barh(categories, values)`
Ch 7.1 Displays a bar chart with bars for each category in a column, with height proportional to the corresponding frequency; `values` argument unnecessary if table has only a column of categories and a column of values `categories` (string): name of the column with categories
`values` (string, optional): the name of the column with values for corresponding categories
None: draws a bar chart
`tbl.hist(column, unit, bins)` Ch 7.2 Generates a histogram of the numerical values in a column. unit and bins are optional arguments, used to label the axes and group the values into intervals (bins), respectively; bins have the form [a, b), where a is included in the bin and b is not `column` (string): name of the column with categories
`unit` (string, optional): units of x-axis
`bins` (array of ints or floats, optional): denotes bin boundaries
None: draws a histogram

### `Table.where` Predicates

Any of these predicates can be negated by adding `not_` in front of them, e.g., `are.not_equal_to(Z)` or `are.not_containing(S)`.

Predicate Description
`are.equal_to(Z)` Equal to `Z`
`are.above(x)` Greater than `x`
`are.above_or_equal_to(x)` Greater than or equal to `x`
`are.below(x)` Less than `x`
`are.below_or_equal_to(x)` Less than or equal to `x`
`are.between(x,y)` Greater than or equal to `x` and less than `y`
`are.between_or_equal_to(x,y)` Greater than or equal to `x`, and less than or equal to `y`
`are.contained_in(A)` Is a substring of `A` (if `A` is a string) or an element of `A` (if `A` is an array)
`are.containing(S)` Contains the string `S`
`are.strictly_between(x,y)` Greater than `x` and less than `y`