Java Code Examples for tech.tablesaw.api.Table#intColumn()

The following examples show how to use tech.tablesaw.api.Table#intColumn() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: DistributionVisualizations.java    From tablesaw with Apache License 2.0 6 votes vote down vote up
public static void main(String[] args) throws Exception {

    Table property = Table.read().csv("../data/sacramento_real_estate_transactions.csv");

    IntColumn sqft = property.intColumn("sq__ft");
    IntColumn price = property.intColumn("price");

    sqft.set(sqft.isEqualTo(0), IntColumnType.missingValueIndicator());
    price.set(price.isEqualTo(0), IntColumnType.missingValueIndicator());

    Plot.show(Histogram.create("Distribution of prices", property.numberColumn("price")));

    Layout layout = Layout.builder().title("Distribution of property sizes").build();
    HistogramTrace trace =
        HistogramTrace.builder(property.numberColumn("sq__ft"))
            .marker(Marker.builder().color("#B10DC9").opacity(.70).build())
            .build();
    Plot.show(new Figure(layout, trace));

    Plot.show(Histogram2D.create("Distribution of price and size", property, "price", "sq__ft"));

    Plot.show(BoxPlot.create("Prices by property type", property, "type", "price"));
  }
 
Example 2
Source File: DistributionVisualizations.java    From tablesaw with Apache License 2.0 6 votes vote down vote up
public static void main(String[] args) throws Exception {

    Table property = Table.read().csv("../data/sacramento_real_estate_transactions.csv");

    IntColumn sqft = property.intColumn("sq__ft");
    IntColumn price = property.intColumn("price");

    sqft.set(sqft.isEqualTo(0), IntColumnType.missingValueIndicator());
    price.set(price.isEqualTo(0), IntColumnType.missingValueIndicator());

    Plot.show(Histogram.create("Distribution of prices", property.numberColumn("price")));

    Layout layout = Layout.builder().title("Distribution of property sizes").build();
    HistogramTrace trace =
        HistogramTrace.builder(property.numberColumn("sq__ft"))
            .marker(Marker.builder().color("#B10DC9").opacity(.70).build())
            .build();
    Plot.show(new Figure(layout, trace));

    Plot.show(Histogram2D.create("Distribution of price and size", property, "price", "sq__ft"));

    Plot.show(BoxPlot.create("Prices by property type", property, "type", "price"));
  }
 
Example 3
Source File: TornadoVisualizations.java    From tablesaw with Apache License 2.0 5 votes vote down vote up
public static void main(String[] args) throws Exception {

    Table tornadoes = Table.read().csv("../data/tornadoes_1950-2014.csv");
    out(tornadoes.structure());
    tornadoes.setName("tornadoes");

    // filter out a bad data point
    tornadoes = tornadoes.where(tornadoes.numberColumn("Start Lat").isGreaterThan(20f));

    IntColumn scale = tornadoes.intColumn("scale");
    scale.set(scale.isEqualTo(-9), IntColumnType.missingValueIndicator());

    Table fatalities1 = tornadoes.summarize("fatalities", sum).by("scale");

    Plot.show(
        HorizontalBarPlot.create(
            "Total fatalities by scale", fatalities1, "scale", "sum [fatalities]"));

    Plot.show(
        PiePlot.create("Total fatalities by scale", fatalities1, "scale", "sum [fatalities]"));

    Table fatalities2 = tornadoes.summarize("fatalities", sum).by("state");

    Plot.show(
        ParetoPlot.createVertical(
            "Total Tornado Fatalities by State", fatalities2, "state", "sum [fatalities]"));

    Table injuries1 = tornadoes.summarize("injuries", mean).by("scale");
    Plot.show(
        HorizontalBarPlot.create(
            "Tornado Injuries by Scale", injuries1, "scale", "mean [injuries]"));
    out(injuries1);
  }
 
Example 4
Source File: CrossTab.java    From tablesaw with Apache License 2.0 5 votes vote down vote up
public static Table percents(Table table, String column1) {
  Table countTable = counts(table, column1);
  Table percentTable = Table.create(countTable.name());
  percentTable.addColumns(countTable.column(0).copy());

  IntColumn countsColumn = countTable.intColumn("Count");
  DoubleColumn pctsColumn = DoubleColumn.create("Percents");
  double sum = countsColumn.sum();
  for (int i = 0; i < countsColumn.size(); i++) {
    pctsColumn.append(countsColumn.getDouble(i) / sum);
  }
  percentTable.addColumns(pctsColumn);
  return percentTable;
}
 
Example 5
Source File: DataFrameJoiner.java    From tablesaw with Apache License 2.0 5 votes vote down vote up
/** Create a reverse index for a given column. */
private Index indexFor(Table table, int colIndex) {
  ColumnType type = table.column(colIndex).type();
  if (type instanceof DateColumnType) {
    return new IntIndex(table.dateColumn(colIndex));
  } else if (type instanceof DateTimeColumnType) {
    return new LongIndex(table.dateTimeColumn(colIndex));
  } else if (type instanceof InstantColumnType) {
    return new LongIndex(table.instantColumn(colIndex));
  } else if (type instanceof TimeColumnType) {
    return new IntIndex(table.timeColumn(colIndex));
  } else if (type instanceof StringColumnType || type instanceof TextColumnType) {
    return new StringIndex(table.stringColumn(colIndex));
  } else if (type instanceof IntColumnType) {
    return new IntIndex(table.intColumn(colIndex));
  } else if (type instanceof LongColumnType) {
    return new LongIndex(table.longColumn(colIndex));
  } else if (type instanceof ShortColumnType) {
    return new ShortIndex(table.shortColumn(colIndex));
  } else if (type instanceof BooleanColumnType) {
    return new ByteIndex(table.booleanColumn(colIndex));
  } else if (type instanceof DoubleColumnType) {
    return new DoubleIndex(table.doubleColumn(colIndex));
  } else if (type instanceof FloatColumnType) {
    return new FloatIndex(table.floatColumn(colIndex));
  }
  throw new IllegalArgumentException("Joining attempted on unsupported column type " + type);
}
 
Example 6
Source File: TornadoVisualizations.java    From tablesaw with Apache License 2.0 5 votes vote down vote up
public static void main(String[] args) throws Exception {

    Table tornadoes = Table.read().csv("../data/tornadoes_1950-2014.csv");
    out(tornadoes.structure());
    tornadoes.setName("tornadoes");

    // filter out a bad data point
    tornadoes = tornadoes.where(tornadoes.numberColumn("Start Lat").isGreaterThan(20f));

    IntColumn scale = tornadoes.intColumn("scale");
    scale.set(scale.isEqualTo(-9), IntColumnType.missingValueIndicator());

    Table fatalities1 = tornadoes.summarize("fatalities", sum).by("scale");

    Plot.show(
        HorizontalBarPlot.create(
            "Total fatalities by scale", fatalities1, "scale", "sum [fatalities]"));

    Plot.show(
        PiePlot.create("Total fatalities by scale", fatalities1, "scale", "sum [fatalities]"));

    Table fatalities2 = tornadoes.summarize("fatalities", sum).by("state");

    Plot.show(
        ParetoPlot.createVertical(
            "Total Tornado Fatalities by State", fatalities2, "state", "sum [fatalities]"));

    Table injuries1 = tornadoes.summarize("injuries", mean).by("scale");
    Plot.show(
        HorizontalBarPlot.create(
            "Tornado Injuries by Scale", injuries1, "scale", "mean [injuries]"));
    out(injuries1);
  }
 
Example 7
Source File: CrossTab.java    From tablesaw with Apache License 2.0 5 votes vote down vote up
public static Table percents(Table table, String column1) {
  Table countTable = counts(table, column1);
  Table percentTable = Table.create(countTable.name());
  percentTable.addColumns(countTable.column(0).copy());

  IntColumn countsColumn = countTable.intColumn("Count");
  DoubleColumn pctsColumn = DoubleColumn.create("Percents");
  double sum = countsColumn.sum();
  for (int i = 0; i < countsColumn.size(); i++) {
    pctsColumn.append(countsColumn.getDouble(i) / sum);
  }
  percentTable.addColumns(pctsColumn);
  return percentTable;
}
 
Example 8
Source File: DataFrameJoiner.java    From tablesaw with Apache License 2.0 5 votes vote down vote up
/** Create a reverse index for a given column. */
private Index indexFor(Table table, int colIndex) {
  ColumnType type = table.column(colIndex).type();
  if (type instanceof DateColumnType) {
    return new IntIndex(table.dateColumn(colIndex));
  } else if (type instanceof DateTimeColumnType) {
    return new LongIndex(table.dateTimeColumn(colIndex));
  } else if (type instanceof InstantColumnType) {
    return new LongIndex(table.instantColumn(colIndex));
  } else if (type instanceof TimeColumnType) {
    return new IntIndex(table.timeColumn(colIndex));
  } else if (type instanceof StringColumnType || type instanceof TextColumnType) {
    return new StringIndex(table.stringColumn(colIndex));
  } else if (type instanceof IntColumnType) {
    return new IntIndex(table.intColumn(colIndex));
  } else if (type instanceof LongColumnType) {
    return new LongIndex(table.longColumn(colIndex));
  } else if (type instanceof ShortColumnType) {
    return new ShortIndex(table.shortColumn(colIndex));
  } else if (type instanceof BooleanColumnType) {
    return new ByteIndex(table.booleanColumn(colIndex));
  } else if (type instanceof DoubleColumnType) {
    return new DoubleIndex(table.doubleColumn(colIndex));
  } else if (type instanceof FloatColumnType) {
    return new FloatIndex(table.floatColumn(colIndex));
  }
  throw new IllegalArgumentException("Joining attempted on unsupported column type " + type);
}
 
Example 9
Source File: BarExample.java    From tablesaw with Apache License 2.0 4 votes vote down vote up
public static void main(String[] args) throws Exception {
  Table table = Table.read().csv("../data/tornadoes_1950-2014.csv");
  NumericColumn<?> logNInjuries = table.numberColumn("injuries").add(1).logN();
  logNInjuries.setName("log injuries");
  table.addColumns(logNInjuries);
  IntColumn scale = table.intColumn("scale");
  scale.set(scale.isLessThan(0), IntColumnType.missingValueIndicator());

  Table summaryTable = table.summarize("fatalities", "log injuries", sum).by("Scale");

  Plot.show(
      HorizontalBarPlot.create(
          "Tornado Impact",
          summaryTable,
          "scale",
          Layout.BarMode.STACK,
          "Sum [Fatalities]",
          "Sum [log injuries]"));

  Plot.show(
      VerticalBarPlot.create(
          "Tornado Impact",
          summaryTable,
          "scale",
          Layout.BarMode.GROUP,
          "Sum [Fatalities]",
          "Sum [log injuries]"));

  Layout layout =
      Layout.builder()
          .title("Tornado Impact")
          .barMode(Layout.BarMode.GROUP)
          .showLegend(true)
          .build();

  String[] numberColNames = {"Sum [Fatalities]", "Sum [log injuries]"};
  String[] colors = {"#85144b", "#FF4136"};

  Trace[] traces = new Trace[2];
  for (int i = 0; i < 2; i++) {
    String name = numberColNames[i];
    BarTrace trace =
        BarTrace.builder(summaryTable.categoricalColumn("scale"), summaryTable.numberColumn(name))
            .orientation(BarTrace.Orientation.VERTICAL)
            .marker(Marker.builder().color(colors[i]).build())
            .showLegend(true)
            .name(name)
            .build();
    traces[i] = trace;
  }
  Plot.show(new Figure(layout, traces));
}
 
Example 10
Source File: BarPieAndParetoExample.java    From tablesaw with Apache License 2.0 4 votes vote down vote up
public static void main(String[] args) throws Exception {

    // ***************** Setup *************************
    // load the data into a table
    Table tornadoes = Table.read().csv("../data/tornadoes_1950-2014.csv");

    // Get the scale column and replace any values of -9 with the column's missing value indicator
    IntColumn scale = tornadoes.intColumn("scale");
    scale.set(scale.isEqualTo(-9), IntColumnType.missingValueIndicator());

    // ***************** Plotting **********************

    // BAR PLOTS

    // Sum the number of fatalities from each tornado, grouping by scale
    Table fatalities1 = tornadoes.summarize("fatalities", sum).by("scale");

    // Plot
    Plot.show(
        HorizontalBarPlot.create(
            "fatalities by scale", // plot title
            fatalities1, // table
            "scale", // grouping column name
            "sum [fatalities]")); // numeric column name

    // Plot the mean injuries rather than a sum.
    Table injuries1 = tornadoes.summarize("injuries", mean).by("scale");

    Plot.show(
        HorizontalBarPlot.create(
            "Average number of tornado injuries by scale", injuries1, "scale", "mean [injuries]"));

    // PIE PLOT
    Plot.show(PiePlot.create("fatalities by scale", fatalities1, "scale", "sum [fatalities]"));

    // PARETO PLOT
    Table t2 = tornadoes.summarize("fatalities", sum).by("State");

    t2 = t2.sortDescendingOn(t2.column(1).name());
    Layout layout = Layout.builder().title("Tornado Fatalities by State").build();
    BarTrace trace = BarTrace.builder(t2.categoricalColumn(0), t2.numberColumn(1)).build();
    Plot.show(new Figure(layout, trace));
  }
 
Example 11
Source File: BarExample.java    From tablesaw with Apache License 2.0 4 votes vote down vote up
public static void main(String[] args) throws Exception {
  Table table = Table.read().csv("../data/tornadoes_1950-2014.csv");
  NumericColumn<?> logNInjuries = table.numberColumn("injuries").add(1).logN();
  logNInjuries.setName("log injuries");
  table.addColumns(logNInjuries);
  IntColumn scale = table.intColumn("scale");
  scale.set(scale.isLessThan(0), IntColumnType.missingValueIndicator());

  Table summaryTable = table.summarize("fatalities", "log injuries", sum).by("Scale");

  Plot.show(
      HorizontalBarPlot.create(
          "Tornado Impact",
          summaryTable,
          "scale",
          Layout.BarMode.STACK,
          "Sum [Fatalities]",
          "Sum [log injuries]"));

  Plot.show(
      VerticalBarPlot.create(
          "Tornado Impact",
          summaryTable,
          "scale",
          Layout.BarMode.GROUP,
          "Sum [Fatalities]",
          "Sum [log injuries]"));

  Layout layout =
      Layout.builder()
          .title("Tornado Impact")
          .barMode(Layout.BarMode.GROUP)
          .showLegend(true)
          .build();

  String[] numberColNames = {"Sum [Fatalities]", "Sum [log injuries]"};
  String[] colors = {"#85144b", "#FF4136"};

  Trace[] traces = new Trace[2];
  for (int i = 0; i < 2; i++) {
    String name = numberColNames[i];
    BarTrace trace =
        BarTrace.builder(summaryTable.categoricalColumn("scale"), summaryTable.numberColumn(name))
            .orientation(BarTrace.Orientation.VERTICAL)
            .marker(Marker.builder().color(colors[i]).build())
            .showLegend(true)
            .name(name)
            .build();
    traces[i] = trace;
  }
  Plot.show(new Figure(layout, traces));
}
 
Example 12
Source File: BarPieAndParetoExample.java    From tablesaw with Apache License 2.0 4 votes vote down vote up
public static void main(String[] args) throws Exception {

    // ***************** Setup *************************
    // load the data into a table
    Table tornadoes = Table.read().csv("../data/tornadoes_1950-2014.csv");

    // Get the scale column and replace any values of -9 with the column's missing value indicator
    IntColumn scale = tornadoes.intColumn("scale");
    scale.set(scale.isEqualTo(-9), IntColumnType.missingValueIndicator());

    // ***************** Plotting **********************

    // BAR PLOTS

    // Sum the number of fatalities from each tornado, grouping by scale
    Table fatalities1 = tornadoes.summarize("fatalities", sum).by("scale");

    // Plot
    Plot.show(
        HorizontalBarPlot.create(
            "fatalities by scale", // plot title
            fatalities1, // table
            "scale", // grouping column name
            "sum [fatalities]")); // numeric column name

    // Plot the mean injuries rather than a sum.
    Table injuries1 = tornadoes.summarize("injuries", mean).by("scale");

    Plot.show(
        HorizontalBarPlot.create(
            "Average number of tornado injuries by scale", injuries1, "scale", "mean [injuries]"));

    // PIE PLOT
    Plot.show(PiePlot.create("fatalities by scale", fatalities1, "scale", "sum [fatalities]"));

    // PARETO PLOT
    Table t2 = tornadoes.summarize("fatalities", sum).by("State");

    t2 = t2.sortDescendingOn(t2.column(1).name());
    Layout layout = Layout.builder().title("Tornado Fatalities by State").build();
    BarTrace trace = BarTrace.builder(t2.categoricalColumn(0), t2.numberColumn(1)).build();
    Plot.show(new Figure(layout, trace));
  }