withCategoricColumnFromRowValues method

DataFrame withCategoricColumnFromRowValues({
  1. required String name,
  2. required String generator(
    1. Map<String, num> numericValues,
    2. Map<String, String> categoricValues
    ),
})

A new data frame with a categoric column generated based on row values.

Example:

final d = """
  a,b
  3,bat
  5,sparrow
 -3,stork
 -5,squirrel
""".parseAsCsv();
print(d);
print(d.withCategoricColumnFromRowValues(
  name: "category",
  generator: (numeric, categoric) =>
    (numeric["a"]! >= 0 ? "positive" : "negative") +
    (["bat", "squirrel"].contains(categoric["b"]!) ? "-mammal": "-bird"),
));

.--.----------.
|a |b         |
:--+----------:
|3 |bat       |
|5 |sparrow   |
|-3|stork     |
|-5|squirrel  |
'--'----------'

.--.----------.-----------------.
|a |b         |category         |
:--+----------+-----------------:
|3 |bat       |positive-mammal  |
|5 |sparrow   |positive-bird    |
|-3|stork     |negative-bird    |
|-5|squirrel  |negative-mammal  |
'--'----------'-----------------'

Implementation

DataFrame withCategoricColumnFromRowValues({
  required String name,
  required String Function(
          Map<String, num> numericValues, Map<String, String> categoricValues)
      generator,
}) {
  if (!_columnNameOkay(name)) {
    throw PackhorseError.badColumnName(name);
  }
  return copy
    ..categoricColumns[name] =
        CategoricColumn(_categoricRowValueResults(generator));
}