flow php

Example: Create columns

Topic: Data frame


Description

To create new columns (row entries) we always use DataFrame::withEntry(string $entryName, ScalarFunction|WindowFunction $ref) method.
We can create new entry by providing a unique $entryName, if the entry already exists it will be replaced.

As a second argument we can provide a static value or a function that will be evaluated for each row.

  • DataFrame::withEntry('number', lit(5)) - creates a new column with a constant value of 5
  • DataFrame::withEntry('is_odd', ref('another_column')->isOdd()) - creates a new column that checks if the value of another_column in is odd

Code

<?php

declare(strict_types=1);

use function Flow\ETL\DSL\{data_frame, from_array, lit, ref, to_stream};

require __DIR__ . '/../../../autoload.php';

data_frame()
    ->read(from_array([
        ['id' => 1, 'name' => 'Norbert'],
        ['id' => 2, 'name' => 'John'],
        ['id' => 3, 'name' => 'Jane'],
    ]))
    ->withEntry('active', ref('id')->isOdd())
    ->withEntry('number', lit(5))
    ->collect()
    ->write(to_stream(__DIR__ . '/output.txt', truncate: false))
    ->run();

Output

+----+---------+--------+--------+
| id |    name | active | number |
+----+---------+--------+--------+
|  1 | Norbert |   true |      5 |
|  2 |    John |  false |      5 |
|  3 |    Jane |   true |      5 |
+----+---------+--------+--------+
3 rows

Contributors

Join us on GitHub external resource
scroll back to top