### Display Summary Output Source: https://jtablesaw.github.io/tablesaw/tutorial Example output format for the summarized tornado data. ```text tornadoes_1950-2014.csv summary Date year | Mean [Date lag(1) - Date[DAYS]] | Count [Date lag(1) - Date[DAYS]] | -------------------------------------------------------------------------------------- 1950 | 2.0555555555555545 | 162 | 1951 | 1.7488584474885829 | 219 | 1952 | 1.8673469387755088 | 196 | 1953 | 0.983870967741935 | 372 | 1954 | 0.8617283950617302 | 405 | ... ``` -------------------------------- ### Crosstab Table Proportions Example Source: https://jtablesaw.github.io/tablesaw/userguide/crosstabs An example output of a crosstab table showing the proportions (percentages) of polls conducted by different pollsters in various months. This table helps visualize the distribution of data. ```text Crosstab Table Proportions: [labels] | fox | gallup | newsweek | time.cnn | upenn | zogby | total | ------------------------------------------------------------------------------------------- APRIL | 1.9% | 3.1% | 0.9% | 0.3% | 0.0% | 0.9% | 7.1% | AUGUST | 0.9% | 2.5% | 0.6% | 0.3% | 0.0% | 0.6% | 5.0% | DECEMBER | 1.2% | 2.8% | 1.2% | 0.9% | 0.6% | 1.5% | 8.4% | FEBRUARY | 2.2% | 2.8% | 1.2% | 1.2% | 0.3% | 1.2% | 9.0% | JANUARY | 2.2% | 4.0% | 1.9% | 0.9% | 1.5% | 2.5% | 13.0% | JULY | 1.9% | 2.8% | 1.2% | 0.9% | 0.0% | 1.2% | 8.0% | JUNE | 1.9% | 3.4% | 0.3% | 0.3% | 0.0% | 1.2% | 7.1% | MARCH | 1.5% | 3.7% | 1.2% | 0.9% | 0.0% | 1.9% | 9.3% | MAY | 1.2% | 2.8% | 1.5% | 0.9% | 0.0% | 0.3% | 6.8% | NOVEMBER | 1.2% | 2.8% | 1.9% | 0.9% | 0.3% | 0.3% | 7.4% | OCTOBER | 2.2% | 3.1% | 2.5% | 0.6% | 0.3% | 0.9% | 9.6% | SEPTEMBER | 1.5% | 3.1% | 2.5% | 0.9% | 0.0% | 1.2% | 9.3% | Total | 19.8% | 36.8% | 17.0% | 9.3% | 3.1% | 13.9% | 100.0% | ``` -------------------------------- ### Applying functions across different column types Source: https://jtablesaw.github.io/tablesaw/userguide/reducing Example of applying specific functions to mixed column types simultaneously. ```Java t.summarize(booleanColumn, numericColumn, standardDeviation, countTrue) ``` -------------------------------- ### Get All Columns as a List Source: https://jtablesaw.github.io/tablesaw/userguide/tables Use `columns()` to retrieve all columns of a table as a list. ```java t.columns(); ``` -------------------------------- ### Import Data from Database Query Source: https://jtablesaw.github.io/tablesaw/userguide/importing_data Reads data from a database query result set into a Table. Column types are inferred from the database. Requires JDBC setup. ```Java String DB_URL = "jdbc:derby:CoffeeDB;create=true"; Connection conn = DriverManager.getConnection(DB_URL); Table customer = null; try (Statement stmt = conn.createStatement()) { String sql = "SELECT * FROM Customer"; try (ResultSet results = stmt.executeQuery(sql)) { customer = Table.read().db(results, "Customer"); } } ``` -------------------------------- ### Create a Basic Scatter Plot Source: https://jtablesaw.github.io/tablesaw/userguide/Visualization_custom Builds a simple scatter plot using two numeric columns. A default layout is used if none is provided. This is a starting point for creating visualizations. ```java Trace trace = ScatterTrace.builder(colX, colY).build(); Plot.show(new Figure(trace)); ``` -------------------------------- ### Combine Multiple Column Filters Source: https://jtablesaw.github.io/tablesaw/userguide/filters Combine filters on different column types (string and number) using `and()` to create a composite query. This example filters by status and age. ```java Table filtered = aTable.where( aTable.stringColumn("Status").isEqualTo("Ok") .and(aTable.numberColumn("Age").isGreaterThan(21))); ``` -------------------------------- ### Get Table Structure Source: https://jtablesaw.github.io/tablesaw/userguide/tables Retrieves a detailed structure of the table, including column names, their order, and data types. This is helpful for understanding the dataset's schema. ```java t.structure() ``` -------------------------------- ### Combine Range and String Filters Source: https://jtablesaw.github.io/tablesaw/userguide/filters Combine a range selection with a string filter using `and()` to narrow down results. This example selects rows within a specific index range that also meet a string condition. ```java Table t1 = t.where(Selection.withRange(100, 300).and(sc.startsWith("Foo"))); ``` -------------------------------- ### Get First N Rows of a Table Source: https://jtablesaw.github.io/tablesaw/userguide/tables Returns a new table containing only the first 'n' rows of the original table. Useful for previewing data. ```java aTable.first(3); ``` -------------------------------- ### Custom Filter Predicate Interface Source: https://jtablesaw.github.io/tablesaw/userguide/filters Defines the abstract method `apply()` for creating custom filter methods on a `Table`. This is the starting point for writing your own column-specific filters. ```java public abstract Selection apply(Table relation); ``` -------------------------------- ### Get Table Shape (Rows and Columns) Source: https://jtablesaw.github.io/tablesaw/userguide/tables Returns the dimensions of the table as a string, indicating the total number of rows and columns. Useful for a quick overview of the dataset size. ```java table.shape() ``` -------------------------------- ### Create Custom Row Comparator Source: https://jtablesaw.github.io/tablesaw/userguide/sorting Defines a custom comparator for VRow objects to enable complex sorting logic. This example sorts based on the 'temp' column in ascending order. ```java Comparator tempComparator = new Comparator() { @Override public int compare(VRow o1, VRow o2) { return Double.compare(o1.getDouble("temp"), o2.getDouble("temp")); } }; ``` -------------------------------- ### Filter DataFrame using Column Predicates Source: https://jtablesaw.github.io/tablesaw/gettingstarted Filter a DataFrame based on conditions applied to a specific column. The example uses StringColumn.startsWith() to filter rows where the 'sc' column begins with 'foo'. ```java StringColumn sc = StringColumn.create("sc", new String[] {"foo", "bar", "baz", "foobar"}); DoubleColumn result = nc.where(sc.startsWith("foo")); ``` -------------------------------- ### Insert Column at Specific Index Source: https://jtablesaw.github.io/tablesaw/userguide/tables Use `addColumn()` to insert a column at a specific index. Column numbering starts at 0. The column must be empty or have the same number of elements as other columns in the table. ```java t.addColumn(3, aColumn); ``` -------------------------------- ### Create a Table with Initial Columns Source: https://jtablesaw.github.io/tablesaw/userguide/tables Create a table and add multiple columns during initialization. Ensure columns are defined before passing them. ```java Table t = Table.create("name", column1, column2, column3...) ``` -------------------------------- ### Conditional Summarization in Tablesaw Source: https://jtablesaw.github.io/tablesaw/userguide/reducing Use summarizeIf() to perform calculations only on data that meets specific criteria. This example counts common suffixes between two columns that are longer than two characters. The column being summarized is not explicitly stated in this example and requires review. ```java t.summarizeIf(c1.suffix(c2).length().isGreaterThan(2), count()) ``` -------------------------------- ### Create a Table with New Columns Source: https://jtablesaw.github.io/tablesaw/gettingstarted Demonstrates how to create a new Table in code and add StringColumn and DoubleColumn to it. This is useful for initializing tables with predefined data. ```java String[] animals = {"bear", "cat", "giraffe"}; double[] cuteness = {90.1, 84.3, 99.7}; Table cuteAnimals = Table.create("Cute Animals") .addColumns( StringColumn.create("Animal types", animals), DoubleColumn.create("rating", cuteness)); ``` -------------------------------- ### Preview table rows Source: https://jtablesaw.github.io/tablesaw/tutorial Returns a new table containing the first n rows. ```java tornadoes.first(3) ``` -------------------------------- ### Iterate and Map Column Values Source: https://jtablesaw.github.io/tablesaw/userguide/columns Demonstrates manual iteration versus column-wise mapping operations. ```Java DateColumn weekLater = DateColumn.create("Week Later"); for (LocalDate date: dates) { weekLater.append(date.plusDays(7)); } ``` ```Java DateColumn weekLater = dates.plusDays(7); ``` -------------------------------- ### Load a CSV file with default settings Source: https://jtablesaw.github.io/tablesaw/userguide/importing_data Use the file method to load a CSV file using default assumptions like comma separation and header presence. ```Java Table t = Table.read().file("myFile.csv"); ``` -------------------------------- ### Infer Column Types from CSV Source: https://jtablesaw.github.io/tablesaw/userguide/importing_data Get an array of inferred column types from a CSV file. This string can be directly pasted into Java code. ```java String types = CsvReader.printColumnTypes("data/bush.csv", true, ',')); System.out.println(types); > ColumnType[] columnTypes = { LOCAL_DATE, // 0 date SHORT_INT, // 1 approval CATEGORY, // 2 who } ``` -------------------------------- ### Load Data for Visualization Source: https://jtablesaw.github.io/tablesaw/userguide/Histograms Initializes the dataset from a CSV file for subsequent plotting operations. ```Java Table property = Table.read().csv("sacremento_real_estate_transactions.csv"); ``` -------------------------------- ### Get a Typed Column (e.g., DoubleColumn) Source: https://jtablesaw.github.io/tablesaw/userguide/tables Use type-specific accessor methods like `doubleColumn()` for convenience. These methods perform the necessary casting for you. ```java DoubleColumn dc = t.doubleColumn(); ``` -------------------------------- ### Import Query Support Functions Source: https://jtablesaw.github.io/tablesaw/gettingstarted Imports necessary static methods for combining query filters using AND, OR, and NOT operations. ```java import static tech.tablesaw.api.QuerySupport.and; import static tech.tablesaw.api.QuerySupport.or; import static tech.tablesaw.api.QuerySupport.not; ``` -------------------------------- ### Calculate Global Mean of Annual Averages Source: https://jtablesaw.github.io/tablesaw/tutorial Computes the average of the annual mean values to get a single representative value for the entire period. ```java summary.nCol(1).mean() // Average days between tornadoes in the summer: 0.5931137164104612 ``` -------------------------------- ### Generate Single Variable Cross-Tabulation Counts Source: https://jtablesaw.github.io/tablesaw/userguide/crosstabs Use xTabCounts() with a single column name to get the frequency counts for each category within that variable. ```java Table whoCounts = table.xTabCounts("who"); ``` ```text Column: who Category | Count | ---------------------- zogby | 45 | upenn | 10 | time.cnn | 30 | fox | 64 | newsweek | 55 | gallup | 119 | ``` -------------------------------- ### Get a Single Column by Name Source: https://jtablesaw.github.io/tablesaw/userguide/tables Use `column()` to retrieve a single column by its name. The returned column may need to be cast to a more specific type. ```java t.column("columnName"); ``` -------------------------------- ### Print First N Rows of a Table Source: https://jtablesaw.github.io/tablesaw/gettingstarted The first(n) method displays the first 'n' rows of the table. This is useful for quickly inspecting the beginning of your dataset. ```java System.out.println(bushTable.first(3)); ``` -------------------------------- ### Create a Table Programmatically Source: https://jtablesaw.github.io/tablesaw/userguide/tables Use this to create an empty table with a specified name. Columns can be added later. ```java Table t = Table.create("name") ``` -------------------------------- ### Import Aggregate Functions Source: https://jtablesaw.github.io/tablesaw/gettingstarted Imports static methods for aggregate functions used in summarizing data. ```java // import aggregate functions. import static tech.tablesaw.aggregate.AggregateFunctions.*; ``` -------------------------------- ### Get Last N Rows of a Table Source: https://jtablesaw.github.io/tablesaw/userguide/tables Returns a new table containing only the last 'n' rows of the original table. Useful for examining recent data. ```java aTable.last(4); ``` -------------------------------- ### Sample X Proportion of Rows Source: https://jtablesaw.github.io/tablesaw/userguide/filters Returns a new table containing a random sample of rows, specified as a proportion of the total table size. The proportion is provided as a double. ```java Table sample = t.sampleX(.40); ``` -------------------------------- ### Multiply Double Column by Another Double Column Source: https://jtablesaw.github.io/tablesaw/gettingstarted Performs element-wise multiplication between two DoubleColumns. The output is a new DoubleColumn. The example also prints the resulting column. ```java DoubleColumn other = DoubleColumn.create("other", new Double[] {10.0, 20.0, 30.0, 40.0}); DoubleColumn newColumn = nc2.multiply(other); System.out.println(newColumn.print()); ``` -------------------------------- ### Summarizing by column name Source: https://jtablesaw.github.io/tablesaw/userguide/reducing Alternative syntax for referring to columns by their string names. ```Java t.summarize(columnName...) ``` -------------------------------- ### Summarize Data with Aggregate Functions Source: https://jtablesaw.github.io/tablesaw/userguide/tables Use `summarize()` with column names and aggregate functions (like `mean`, `median`, `range`) to compute summary statistics. `apply()` returns the results as a new table. ```java Table summary = t.summarize("age", "weight", mean, median, range).apply(); ``` -------------------------------- ### Create and Display Simple Scatter Plot Source: https://jtablesaw.github.io/tablesaw/userguide/ScatterPlots Generates and displays a basic scatter plot showing the relationship between 'mean retail' price and 'vintage' for champagne. Requires the 'champagne' table filtered previously. ```java Plot.show( ScatterPlot.create("Champagne prices by vintage", champagne, "mean retail", "vintage")); ``` -------------------------------- ### Filtering a Number Column Source: https://jtablesaw.github.io/tablesaw/userguide/Table%20processing%20without%20loops Create a new column containing only the elements from the original number column that satisfy a given condition. The example filters for values less than 4. ```java NumberColumn filtered = numberColumn.where(numberColumn.isLessThan(4)); ``` -------------------------------- ### Create New Table with Selected Columns Source: https://jtablesaw.github.io/tablesaw/userguide/tables Use `select()` to create a new table containing only the specified columns, in the order provided. This is useful for creating subsets of data. ```java Table reduced = t.select("Name", "Age", "Height", "Weight"); ``` -------------------------------- ### Customize Tick Settings Source: https://jtablesaw.github.io/tablesaw/userguide/Visualization_custom Customize tick appearance by setting color and placement. Requires importing TickSettings and Placement. ```Java TickSettings ticks = TickSettings.builder() .color("red") .placement(Placement.OUTSIDE) .build(); Axis yAxis = Axis.builder().tickSettings(ticks).build(); // etc. ``` -------------------------------- ### Summarize Calculated Columns in Tablesaw Source: https://jtablesaw.github.io/tablesaw/userguide/reducing Use this method to calculate statistics on a column that is derived from a calculation, such as sentence length. No special setup is required beyond having the calculated column available. ```java t.summarize(sentence.length(), min, q1, q2, q3, max, range) ``` -------------------------------- ### Set name and print table Source: https://jtablesaw.github.io/tablesaw/userguide/reducing Assigns a descriptive name to the resulting table and prints it to the console. ```Java avgInjuries.setName("Average injuries by Tornado Scale"); avgInjuries.print(); ``` -------------------------------- ### Create and Print a DoubleColumn Source: https://jtablesaw.github.io/tablesaw/gettingstarted Initialize a DoubleColumn from an array and display its contents. ```java double[] numbers = {1, 2, 3, 4}; DoubleColumn nc = DoubleColumn.create("nc", numbers); System.out.println(nc.print()); ``` ```java Column: nc 1 2 3 4 ``` -------------------------------- ### Select Columns by Name Source: https://jtablesaw.github.io/tablesaw/userguide/tables Use `columns()` with column names to select specific columns from a table. ```java t.columns("column1", "column2"); ``` -------------------------------- ### Create a Line Plot Source: https://jtablesaw.github.io/tablesaw/userguide/Visualization_custom Create a line plot by setting the mode to LINE in ScatterTrace. Requires ScatterTrace, Plot, and Figure classes. ```Java double[] x = {1, 2, 3, 4, 5, 6}; double[] y = {0, 1, 6, 14, 25, 39}; String[] labels = {"a", "b", "c", "d", "e", "f"}; ScatterTrace trace = ScatterTrace.builder(x, y) .mode(ScatterTrace.Mode.LINE) .text(labels) .build(); ``` -------------------------------- ### Load Financial Data from CSV Source: https://jtablesaw.github.io/tablesaw/userguide/TimeSeries Loads a CSV file containing financial data into a Tablesaw Table. Ensure the file path is correct. ```Java Table priceTable = Table.read().csv("../data/ohlcdata.csv"); ``` -------------------------------- ### Get and Manipulate Table Columns Source: https://jtablesaw.github.io/tablesaw/gettingstarted Retrieve all column names or column objects from a table. Columns can be removed by name or kept based on a list of names. You can also remove columns that contain missing values. New columns can be added to the table. ```java List columnNames = table.columnNames(); // returns all column names List> columns = table.columns(); // returns all the columns in the table // removing columns table.removeColumns("Foo"); // keep everything but "foo" table.retainColumns("Foo", "Bar"); // only keep foo and bar table.removeColumnsWithMissingValues(); // adding columns table.addColumns(column1, column2, column3); ``` -------------------------------- ### Calculating multiple statistics for a single column Source: https://jtablesaw.github.io/tablesaw/userguide/reducing Demonstrates calculating mean, max, and min for a specific column using static imports from AggregateFunctions. ```Java import static tech.tablesaw.aggregate.AggregateFunctions.*; ... NumberColumn age = t.nCol("age"); t.summarize(age, mean, max, min); ``` -------------------------------- ### Create Pareto Chart of Fatalities by US State Source: https://jtablesaw.github.io/tablesaw/userguide/BarsAndPies Generates a Pareto chart that displays the sum of tornado fatalities by US state, sorted in descending order. This helps identify the states with the highest fatality counts. ```java Table fatalitiesByState = tornadoes.summarize("fatalities", AggregationFunctions.sum, "state"); // Create and display the Pareto plot fataliiesByState.plot("state", "fatalities").title("Fatalities by State").render(); ``` -------------------------------- ### Display First Rows of Sorted Table Source: https://jtablesaw.github.io/tablesaw/userguide/sorting Displays the first 8 rows of a table. This is useful for quickly inspecting the results of a sort operation. ```java ascending.first(8); ``` -------------------------------- ### Create Two-Dimensional Histograms Source: https://jtablesaw.github.io/tablesaw/userguide/Histograms Visualizes the relationship between two numerical distributions. ```Java Plot.show( Histogram2D.create("Distribution of price and size", property, "price", "sq__ft")); ``` -------------------------------- ### Create Bar Chart of Fatalities by Tornado Scale Source: https://jtablesaw.github.io/tablesaw/userguide/BarsAndPies Generates a bar chart showing the sum of tornado fatalities grouped by tornado intensity scale. Requires summarizing the data first. ```java Table fatalitiesByScale = tornadoes.summarize("fatalities", AggregationFunctions.sum, "scale"); // Create and display the bar plot fatalitiesByScale.plot("scale", "fatalities").title("Fatalities by Scale").render(); ``` -------------------------------- ### Display Table Contents Source: https://jtablesaw.github.io/tablesaw/userguide/tables Print a formatted string representation of the entire table. By default, it shows the first and last ten records. ```java aTable.print(); ``` -------------------------------- ### Customize CSV loading with CsvReadOptions Source: https://jtablesaw.github.io/tablesaw/userguide/importing_data Configure specific parsing parameters such as custom separators, header presence, and date formats using the builder pattern. ```Java CsvReadOptions.Builder builder = CsvReadOptions.builder("myFile.csv") .separator('\t') // table is tab-delimited .header(false) // no header .dateFormat("yyyy.MM.dd"); // the date format to use. CsvReadOptions options = builder.build(); Table t1 = Table.read().usingOptions(options); ``` -------------------------------- ### String Column Transformations Source: https://jtablesaw.github.io/tablesaw/gettingstarted Demonstrates various map functions for StringColumns, including replacing substrings, converting to uppercase, padding, and extracting substrings. It also shows calculating Levenshtein distance between two StringColumns. ```java StringColumn s = StringColumn.create("sc", new String[] {"foo", "bar", "baz", "foobarbaz"}); StringColumn s2 = s.copy(); s2 = s2.replaceFirst("foo", "bar"); s2 = s2.upperCase(); s2 = s2.padEnd(5, 'x'); // put 4 x chars at the end of each string s2 = s2.substring(1, 5); // this returns a measure of the similarity (levenshtein distance) between two columns DoubleColumn distance = s.distance(s2); ``` -------------------------------- ### Group on constant time ranges Source: https://jtablesaw.github.io/tablesaw/userguide/reducing Groups data into 15-minute time windows to summarize sales amounts. ```Java t.summarize(amount, sales_datetime.timeWindows(ChronoUnit.MINUTE, 15) ``` -------------------------------- ### General summarize() syntax Source: https://jtablesaw.github.io/tablesaw/userguide/reducing The basic structure for invoking the summarize method on a table. ```Java t.summarize(column, functions...) ``` -------------------------------- ### Create a Selection for a Specific Year Source: https://jtablesaw.github.io/tablesaw/userguide/filters Demonstrates creating a `Selection` object to filter dates within a specific year using the `isInYear()` method. This selection can then be applied to a `DateColumn` or a `Table`. ```Jave DateColumn bd = student.dateColumn("birth date"); Selection bdYear = bd.isInYear(2011); ``` -------------------------------- ### Create Box Plots Source: https://jtablesaw.github.io/tablesaw/userguide/Histograms Compares distributions across different sub-groups within the dataset. ```Java Plot.show(BoxPlot.create("Prices by property type", property, "type", "price")); ``` -------------------------------- ### Create Pie Chart of Fatalities by Tornado Scale Source: https://jtablesaw.github.io/tablesaw/userguide/BarsAndPies Generates a pie chart to visualize the distribution of tornado fatalities across different intensity scales. This provides an alternative to bar charts for showing proportions. ```java Table fatalitiesByScale = tornadoes.summarize("fatalities", AggregationFunctions.sum, "scale"); // Create and display the pie plot fataliiesByScale.plot("scale", "fatalities").title("Fatalities by Scale").render(); ``` -------------------------------- ### Applying summary functions to generate a result table Source: https://jtablesaw.github.io/tablesaw/userguide/reducing Uses the apply() method to convert the summary result into a Table object. ```Java Table results = t.summarize(approval, mean, max, min).apply(); ``` -------------------------------- ### Create a Scatter Plot Source: https://jtablesaw.github.io/tablesaw/userguide/Visualization_custom Generate a scatter plot with data points and associated labels. Requires ScatterTrace, Plot, and Figure classes. ```Java double[] x = {1, 2, 3, 4, 5, 6}; double[] y = {0, 1, 6, 14, 25, 39}; String[] labels = {"a", "b", "c", "d", "e", "f"}; ScatterTrace trace = ScatterTrace.builder(x, y) .text(labels) .build(); Plot.show(new Figure(trace)); ``` -------------------------------- ### Overlay Histograms Source: https://jtablesaw.github.io/tablesaw/userguide/Visualization_custom Overlay two histograms by setting opacity and using a layout with barMode set to OVERLAY. Requires HistogramTrace, Plot, Figure, and Layout classes. ```Java double[] y1 = {1, 4, 9, 16, 11, 4, 0, 20, 4, 7, 9, 12, 8, 6, 28, 12}; double[] y2 = {3, 11, 19, 14, 11, 14, 5, 24, -4, 10, 15, 6, 5, 18}; HistogramTrace trace1 = HistogramTrace.builder(y1).opacity(.75).build(); HistogramTrace trace2 = HistogramTrace.builder(y2).opacity(.75).build(); Layout layout = Layout.builder() .barMode(Layout.BarMode.OVERLAY) .build(); Plot.show(new Figure(layout, trace1, trace2)); ``` -------------------------------- ### Create OHLC Chart Source: https://jtablesaw.github.io/tablesaw/userguide/TimeSeries Generates an OHLC (Open-High-Low-Close) chart from a Tablesaw Table. Requires a date column and OHLC price columns. ```Java Plot.show(OHLCPlot.create("Prices", // The plot title priceTable, // the table we loaded earlier "date", // our time variable "open", // the price data... "high", "low", "close")); ``` -------------------------------- ### Filter Columns with Selections Source: https://jtablesaw.github.io/tablesaw/gettingstarted Use selection methods to create filters and apply them to columns. ```java nc.isLessThan(3); ``` ```java DoubleColumn filtered = nc.where(nc.isLessThan(3)); Column: nc 1 2 ``` ```java DoubleColumn filteredPositive = nc.where(nc.isLessThan(3).and(nc.isPositive())); ``` -------------------------------- ### Display table content Source: https://jtablesaw.github.io/tablesaw/tutorial Prints the string representation of the table to standard output. ```java System.out.println(tornadoes); ``` -------------------------------- ### Sample N Rows Source: https://jtablesaw.github.io/tablesaw/userguide/filters Returns a new table containing a specified number of randomly sampled rows from the original table. Useful for creating subsets for analysis. ```java Table sample = t.sampleN(50); ``` -------------------------------- ### Create New Table Excluding Columns Source: https://jtablesaw.github.io/tablesaw/userguide/tables Use `rejectColumns()` to create a new table excluding the specified columns. The order of columns in the resulting table is the same as in the original. ```java Table reduced = t.rejectColumns("Street Address"); ``` -------------------------------- ### Specify Date, Time, and Locale Formats Source: https://jtablesaw.github.io/tablesaw/userguide/importing_data Configure locale and specific formats for parsing dates, times, and datetimes to handle variations in CSV files. ```java Table t = Table.read().usingOptions( CsvReadOptions.builder("myFile.csv") .locale(Locale.FRENCH) .dateFormat("yyyy.MM.dd") .timeFormat("HH:mm:ss) .dateTimeFormat("yyyy.MM.dd::HH:mm:ss")); ``` -------------------------------- ### Create DoubleColumn with Values Source: https://jtablesaw.github.io/tablesaw/userguide/columns Initializes a DoubleColumn with a given name and an array of double values. This is useful for creating columns with pre-defined data. ```Java double[] values = {1, 2, 3, 7, 9.44242, 11}; DoubleColumn column = DoubleColumn.create("my numbers", values); ``` -------------------------------- ### Configure Grid Appearance for Axis Source: https://jtablesaw.github.io/tablesaw/userguide/Visualization_custom Sets custom grid colors and widths for an axis. By default, a pale grid is shown, but this can be controlled or turned off. ```java Axis xAxis = Axis.builder() .gridColor("grey") .gridWidth(1) .build(); ``` -------------------------------- ### Print Table Structure Source: https://jtablesaw.github.io/tablesaw/gettingstarted The structure() method returns a table describing the columns of the original table, including their names, types, and indices. This is helpful for understanding the schema of your data. ```java System.out.println(bushTable.structure()); ``` -------------------------------- ### Generate Cross-Tabulation Table Source: https://jtablesaw.github.io/tablesaw/gettingstarted Creates a cross-tabulation table showing the proportions of observations across two categorical columns. The output can be formatted to display percentages. ```java Table percents = table.xTabTablePercents("month", "who"); // make table print as percents with no decimals instead of the raw doubles it holds percents.columnsOfType(ColumnType.DOUBLE) .forEach(x -> ((DoubleColumn)x).setPrintFormatter(NumberColumnFormatter.percent(0))); System.out.println(percents); ``` -------------------------------- ### Create a Bar Plot Source: https://jtablesaw.github.io/tablesaw/userguide/Visualization_custom Generate a bar plot with categorical x-axis labels and numerical y-axis values. Requires BarTrace, Plot, and Figure classes. ```Java Object[] x = {"sheep", "cows", "fish", "tree sloths"}; double[] y = {1, 4, 9, 16}; BarTrace trace = BarTrace.builder(x, y).build(); Plot.show(new Figure(trace)); ``` -------------------------------- ### Create Bar Chart of Mean Injuries by Tornado Scale Source: https://jtablesaw.github.io/tablesaw/userguide/BarsAndPies Generates a bar chart displaying the average number of injuries for each tornado intensity scale. Uses the summarize method with the mean aggregation function. ```java Table meanInjuriesByScale = tornadoes.summarize("injuries", AggregationFunctions.mean, "scale"); // Create and display the bar plot meanInjuriesByScale.plot("scale", "injuries").title("Mean Injuries by Scale").render(); ``` -------------------------------- ### Generate Cross-Tabulation Counts of State and Scale Source: https://jtablesaw.github.io/tablesaw/tutorial Creates a cross-tabulation of counts showing the interaction between US state and tornado scale. Displays the first 10 rows of the resulting cross-tabulation. ```java CrossTab.counts(tornadoes, tornadoes.stringColumn("State"), tornadoes.intColumn("Scale")) .first(10) ``` -------------------------------- ### Apply where() and dropWhere() to Tables Source: https://jtablesaw.github.io/tablesaw/userguide/filters Use `where()` to select rows that pass a filter criteria and `dropWhere()` to exclude rows that pass the criteria. Tablesaw preserves the original order of rows. ```java Table t = table.where(aSelection); Column x = column.where(aSelection); ``` ```java table.dropWhere(aSelection) ``` -------------------------------- ### Summarize Data by Columns Source: https://jtablesaw.github.io/tablesaw/gettingstarted Calculates summary statistics (mean, sum, min, max) for a specified column and groups the results by other columns. ```java Table summary = table.summarize("sales", mean, sum, min, max).by("province", "status"); ``` -------------------------------- ### Create Scatter Plot with Categorical Grouping Source: https://jtablesaw.github.io/tablesaw/userguide/ScatterPlots Creates a scatter plot visualizing 'Mean Retail' against 'highest pro score', with data points colored by 'wine type'. This plot helps identify relationships across different wine categories. ```java Plot.show( ScatterPlot.create("Wine prices and ratings", wines, "Mean Retail", "highest pro score", "wine type")); ``` -------------------------------- ### Select Rows by Index Source: https://jtablesaw.github.io/tablesaw/gettingstarted Use Selection.with() to select rows by specific indexes or Selection.withRange() for a range of indexes. This is useful for retrieving specific data points. ```java nc.where(Selection.with(0, 2)); // returns 2 rows with the given indexes ``` ```java nc.where(Selection.withRange(1, 3)); // returns rows 1 inclusive to 3 exclusive ``` -------------------------------- ### Summarizing multiple columns Source: https://jtablesaw.github.io/tablesaw/userguide/reducing Syntax variants for summarizing multiple columns in a single method call. ```Java t.summarize(column1, column2, function...) t.summarize(column1, column2, column3, function...) t.summarize(column1, column2, column3, column4, function...) ``` -------------------------------- ### Generate Table Percents Source: https://jtablesaw.github.io/tablesaw/userguide/crosstabs Generates a crosstab table showing the percentage of data falling into each combination of two variables. Formats the resulting percentage columns to display a single fractional digit. ```java Table tablePercents = table.xTabTablePercents("month", "who"); tablePercents .columnsOfType(ColumnType.DOUBLE) .forEach(x -> ((NumberColumn) x).setPrintFormatter(NumberColumnFormatter.percent(1))); ``` -------------------------------- ### Create Line Plot for Observations Source: https://jtablesaw.github.io/tablesaw/userguide/TimeSeries Generates a simple line plot to visualize observations in chronological order. Requires 'Record' and 'Robberies' columns. ```java Table robberies = Table.read().csv("../data/boston-robberies.csv"); Plot.show( LinePlot.create("Monthly Boston Robberies: Jan 1966-Oct 1975", robberies, "Record", "Robberies")); ``` -------------------------------- ### Calculate Column Percentages Source: https://jtablesaw.github.io/tablesaw/userguide/crosstabs Use xTabColumnPercents to calculate the distribution of values within each column of a crosstab. ```java Table columnPercents = table.xTabColumnPercents("month", "who"); ``` -------------------------------- ### View table metadata Source: https://jtablesaw.github.io/tablesaw/tutorial Methods to inspect column names, table dimensions, and structure. ```java tornadoes.columnNames() ``` ```java tornadoes.shape() ``` ```java tornadoes.structure().printAll() ``` -------------------------------- ### Import Data from CSV File Source: https://jtablesaw.github.io/tablesaw/userguide/tables Load data into a table from a CSV file. This method uses default settings for separators and attempts to infer column types. For advanced options, refer to the importing data documentation. ```java Table t = Table.read().csv("myFile.csv"); ``` -------------------------------- ### Group on calculated columns Source: https://jtablesaw.github.io/tablesaw/userguide/reducing Uses a map function to bin numeric data into 20 groups before calculating the mean salary. ```Java t.summarize(salary, mean).by(yearsOfEmployment.bin(20)); ``` -------------------------------- ### Combine Selections with AND Source: https://jtablesaw.github.io/tablesaw/userguide/filters Use the `and()` method to combine two selection criteria, requiring both to be true for a row to be selected. This is useful for creating complex queries. ```java bd.isInJanuary().and(bd.isMonday()) ``` -------------------------------- ### Group on standard time units Source: https://jtablesaw.github.io/tablesaw/userguide/reducing Summarizes sales data by region and the month extracted from a datetime column. ```Java t.summarize(amount, max).by(region, sales_datetime.month()) ``` -------------------------------- ### Create One-Dimensional Histograms Source: https://jtablesaw.github.io/tablesaw/userguide/Histograms Generates histograms to visualize the distribution of numerical data within a column. ```Java Plot.show(Histogram.create("Distribution of prices", property, "price")); ``` ```Java NumberColumn sqft = property.numberColumn("sq__ft"); sqft.set(sqft.isEqualTo(0), DoubleColumnType.missingValueIndicator()); Plot.show(Histogram.create("Distribution of property sizes", property, "sq__ft")); ``` -------------------------------- ### Create Custom Map Functions with doWithEach Source: https://jtablesaw.github.io/tablesaw/userguide/mapping Use doWithEach to apply custom logic to column values. Handling missing data is recommended. ```java Table table = Table.read().csv("../data/bush.csv"); DateColumn dc1 = table.dateColumn("date"); DateColumn dc2 = DateColumn.create("100 days later"); dc1.doWithEach(localDate -> dc2.append(localDate.plusDays(100))); ``` ```java DateColumn dc2 = DateColumn.create("100 days later"); dc1.doWithEach(new Consumer() { @Override public void accept(LocalDate localDate) { if (localDate == null) { dc2.appendMissing(); } else { dc2.append(localDate.plusDays(100)); } } }); ``` -------------------------------- ### Split Table into Random Subsets Source: https://jtablesaw.github.io/tablesaw/userguide/filters Divides the table into two random subsets. The proportion for the first subset is specified, and the second subset contains the remaining rows. Useful for machine learning training/testing splits. ```java Table[] results = Table.sampleSplit(.333); ``` -------------------------------- ### Calculate Cross-Tabulation Percents Source: https://jtablesaw.github.io/tablesaw/userguide/crosstabs Use xTabPercents() to calculate the proportion of observations for each category. The output is in decimal format and can be formatted as percentages. ```java Table whoPercents = table.xTabPercents("who"); ``` ```java whoPercents .columnsOfType(ColumnType.DOUBLE) // format to display as percents .forEach(x -> ((NumberColumn) x).setPrintFormatter(NumberColumnFormatter.percent(0))); ``` ```text Column: who Category | Percents | ------------------------- zogby | 14% | upenn | 3% | time.cnn | 9% | fox | 20% | newsweek | 17% | gallup | 37% | ``` -------------------------------- ### Add Columns to a Table Source: https://jtablesaw.github.io/tablesaw/userguide/tables Use `addColumns()` to add one or more columns to an existing table. The columns must be empty or have the same number of elements as other columns in the table. ```java t.addColumns(aColumn...) ``` -------------------------------- ### Date and DateTime Filters Source: https://jtablesaw.github.io/tablesaw/userguide/filters Methods for filtering Date and DateTime columns based on specific dates, years, quarters, months, and days of the week. ```APIDOC ## Date and DateTime Filters ### Description Filters for Date and DateTime columns to isolate records based on temporal criteria. ### Methods - equalTo(LocalDate date) - before(LocalDate date) - after(LocalDate date) - inYear(int fourDigitYear) - inQ1(), inQ2(), inQ3(), inQ4() - inJanuary(), inFebruary(), ..., inDecember() - sunday(), monday(), ..., saturday() - firstDayOfMonth() - lastDayOfMonth() ``` -------------------------------- ### Standard Column Operations Source: https://jtablesaw.github.io/tablesaw/userguide/columns Common methods available on most column types for inspection and transformation. ```Java name() // returns the name of the column type() // returns the ColumnType, e.g. LOCAL_DATE size() // returns the number of elements isEmpty() // returns true if column has no data; false otherwise first(n) and last(n) // returns the first and last n elements max() and min() // returns the largest and smallest elements top(n) and bottom(n) // returns the n largest and smallest elements print() // returns a String representation of the column copy() // returns a deep copy of the column emptyCopy() // returns a column of the same type and name, but no data unique() // returns a column of only the unique values countUnique() // returns the number of unique values asSet() // returns the unique values as a java Set summary() // returns a type specific summary of the data void sortAscending() // sorts the column in ascending order void sortDescending() // sorts the column in ascending order append(value) // appends a single value to the column appendCell(string) // converts the string to the correct type and appends the result append(otherColumn) // Appends the data in other column to this one removeMissing() // returns a column with all missing values removed ``` -------------------------------- ### Create 3D Scatter Plot with Five Variables Source: https://jtablesaw.github.io/tablesaw/userguide/ScatterPlots Generates a 3D scatter plot visualizing five variables: 'vintage' (x), 'highest pro score' (y), 'highest retail' (z), 'lowest retail' (point size), and 'appellation' (category). This provides a comprehensive view of multiple data dimensions. ```java Plot.show( Scatter3DPlot.create("High & low retail price for champagne by vintage and rating", champagne, "vintage", "highest pro score", "highest retail", "lowest retail", "appellation")); ``` -------------------------------- ### Iterate Over Columns Source: https://jtablesaw.github.io/tablesaw/userguide/mapping Columns are iterable, allowing for custom logic using standard for-loops. ```java StringColumn season = StringColumn.create("Season"); for (LocalDate date : dateColumn) { if (date == null) { newColumn.append(StringColumn.MISSING_VALUE); } else if(date.month.equals("May") { newColumn.append("Flower Season"); } else { newColumn.append("Not Flower Season"); } } myTable.addColumns(season); ``` -------------------------------- ### Add Tablesaw Dependency Source: https://jtablesaw.github.io/tablesaw/gettingstarted Include this dependency in your Maven pom.xml file to use Tablesaw. ```xml tech.tablesaw tablesaw-core LATEST ``` -------------------------------- ### Configure Spikes and Hover Mode Source: https://jtablesaw.github.io/tablesaw/userguide/Visualization_custom Creates vertical spikes for a plot and sets the hover mode to CLOSEST, which is required for spikes to function. Spikes are lines drawn from a data point to an axis on hover. ```java Spikes spikes = Spikes.builder() .dash("solid") .color("yellow") .build(); Axis xAxis = Axis.builder() .spikes(spikes) .build(); Layout layout = Layout.builder() .xAxis(xAxis) .hoverMode(HoverMode.CLOSEST) .build(); Trace trace = ScatterTrace.builder(xCol, yCol).build(); Figure plot = new Figure(layout, trace); ``` -------------------------------- ### Add Tablesaw Excel Dependency Source: https://jtablesaw.github.io/tablesaw/userguide/importing_data Include this Maven dependency to enable importing data from Excel files. ```XML tech.tablesaw tablesaw-excel ``` -------------------------------- ### Create a Plot with Custom Layout Source: https://jtablesaw.github.io/tablesaw/userguide/Visualization_custom Constructs a scatter plot with a custom layout, including a title, height, and width. This allows for more control over the plot's appearance. ```java Layout layout = Layout.builder() .title("My Title") .height(500) .width(650) .build(); Trace trace = ScatterTrace.builder(colX, colY).build(); Plot.show(new Figure(layout, trace)); ``` -------------------------------- ### Add Tablesaw HTML Dependency Source: https://jtablesaw.github.io/tablesaw/userguide/importing_data Include this Maven dependency to enable importing data from HTML files. ```XML tech.tablesaw tablesaw-html ``` -------------------------------- ### Calculate Row Percentages Source: https://jtablesaw.github.io/tablesaw/userguide/crosstabs Use xTabRowPercents to calculate the distribution of values across each row of a crosstab. ```java Table rowPercents = table.xTabRowPercents("month", "who"); ``` -------------------------------- ### Export Table to CSV Source: https://jtablesaw.github.io/tablesaw/tutorial Writes the contents of a table to a CSV file on the local filesystem. ```java tornadoes.write().csv("rev_tornadoes_1950-2014.csv"); ``` -------------------------------- ### Summarize Tornado Frequency by Year Source: https://jtablesaw.github.io/tablesaw/tutorial Aggregates the delta column to calculate the mean and count of intervals, grouped by the year of the tornado. ```java Table summary = summer.summarize(delta, mean, count).by(summerDate.year()); ``` -------------------------------- ### Load and Clean Tornado Dataset in Tablesaw Source: https://jtablesaw.github.io/tablesaw/userguide/BarsAndPies Loads a CSV dataset and cleans the 'scale' column by replacing missing values (-9) with a missing-value indicator. This prepares the data for plotting. ```java Table tornadoes = Table.read().csv("data/tornadoes.csv"); // Missing values in scale column are encoded as -9. Replace them with a missing value indicator. ColumnData scale = tornadoes.intColumn("scale").setMissing(-9, Integer.MISSING); tornadoes.addColumns(scale); ``` -------------------------------- ### Summarize Data Grouped by a Column Source: https://jtablesaw.github.io/tablesaw/userguide/tables Use `summarize()` followed by `by()` to calculate summary statistics for groups defined by one or more columns. This is useful for calculating subtotals. ```java Table result = t.summarize("delay", mean).by("airport"); ``` -------------------------------- ### Create a Box Plot Source: https://jtablesaw.github.io/tablesaw/userguide/Visualization_custom Generate a box plot using categorical x-axis data and numerical y-axis data. Requires BoxTrace, Plot, and Figure classes. ```JavaScript Object[] x = {"sheep", "cows", "fish", "tree sloths", "sheep", "cows", "fish", "tree sloths", "sheep", "cows", "fish", "tree sloths"}; double[] y = {1, 4, 9, 16, 3, 6, 8, 8, 2, 4, 7, 11}; BoxTrace trace = BoxTrace.builder(x, y).build(); Plot.show(new Figure(trace)); ``` -------------------------------- ### Process Rows in Batches with Rolling or Stepping Streams Source: https://jtablesaw.github.io/tablesaw/gettingstarted For processing multiple rows at a time, Tablesaw provides `rollingStream()` and `steppingStream()`. `rollingStream()` processes overlapping sets of rows, while `steppingStream()` processes distinct, non-overlapping sets of rows. ```java // Consumer prints out the max of a window. Consumer consumer = rows -> System.out.println(Arrays.stream(rows).mapToDouble(row -> row.getDouble(0)).max()); // Streams over rolling sets of rows. I.e. 0 to n-1, 1 to n, 2 to n+1, etc. table.rollingStream(3).forEach(consumer); // Streams over stepped sets of rows. I.e. 0 to n-1, n to 2n-1, 2n to 3n-1, etc. Only returns // full sets of rows. table.steppingStream(5).forEach(consumer); ``` -------------------------------- ### Concatenate Tables by Adding Columns Source: https://jtablesaw.github.io/tablesaw/userguide/tables Use `concat()` to combine two tables by adding the columns of the second table to the first. This requires both tables to have the same number of rows. The method modifies the receiver table. ```java t.concat(t2); ``` -------------------------------- ### Summarize Data by Date Part Source: https://jtablesaw.github.io/tablesaw/gettingstarted Calculates summary statistics (mean, median) for a column, grouping by a specific part of a date column, such as the day of the week. ```java summary = table.summarize("sales", mean, median) .by(table.dateColumn("sales date").dayOfWeek()); ``` -------------------------------- ### Create Empty DateColumn Source: https://jtablesaw.github.io/tablesaw/userguide/columns Creates an empty DateColumn with a specified name. The column type is automatically set to LOCAL_DATE. Column names must be unique within a table. ```Java DateColumn column = DateColumn.create("test"); ``` -------------------------------- ### Mapping a String Column Source: https://jtablesaw.github.io/tablesaw/userguide/Table%20processing%20without%20loops Use this for applying a transformation to each element in a string column. Ensure the column is of type StringColumn. ```java StringColumn sc = myStringColumn.concat("foo"); ``` -------------------------------- ### Calculate Descriptive Statistics for a Column Source: https://jtablesaw.github.io/tablesaw/tutorial Calculates and prints descriptive statistics for a specified column. Ensure the column exists and is of a suitable numeric type. ```java tornadoes.column("Fatalities").summary().print() ``` -------------------------------- ### Filter StringColumn using contains(), startsWith(), and endsWith() Source: https://jtablesaw.github.io/tablesaw/userguide/filters Illustrates filtering a `StringColumn` using built-in selection methods like `contains()`, `startsWith()`, and `endsWith()`. These methods return a `Selection` object that can be applied to filter the column or table. ```java filtered1 = unfiltered.selectif(column("name").contains("charles")); ``` ```java StringColumn email = unfiltered.stringColumn("email"); filtered = unfiltered.where(email.endsWith("google.com")); ``` -------------------------------- ### Read and Filter Wine Data Source: https://jtablesaw.github.io/tablesaw/userguide/ScatterPlots Reads a CSV file into a Table and filters it to select 'Champagne & Sparkling' wines from the 'California' region. This is a prerequisite for plotting specific wine data. ```java Table wines = Table.read().csv("wine_test.csv"); Table champagne = wines.where( wines.stringColumn("wine type").isEqualTo("Champagne & Sparkling") .and(wines.stringColumn("region").isEqualTo("California"))); ```