flow php

Example: Rename columns

Topic: Data frame


Description

There are multiple ways to rename entries in a DataFrame:

  • rename(string $from, string $to) - renames a single entry
  • renameAll(string $search, string $replace) - renames all entries that contain a given substring and replaces it with another substring
  • renameAllToLowercase() - renames all entries to lowercase
  • renameAllStyle(StringStyles|string $style) - renames all entries to a given style (e.g. camel, snakem, kebab, etc.)

Code

<?php

declare(strict_types=1);

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

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

data_frame()
    ->read(from_array([
        ['id' => 1, 'name' => 'Norbert', 'joined_id' => 1, 'joined_status' => 'active'],
        ['id' => 2, 'name' => 'John', 'joined_id' => 2, 'joined_status' => 'inactive'],
        ['id' => 3, 'name' => 'Jane', 'joined_id' => 3, 'joined_status' => 'active'],
    ]))
    ->rename('id', 'user_id')
    ->renameAll('joined_', '')
    ->collect()
    ->write(to_stream(__DIR__ . '/output.txt', truncate: false))
    ->run();

Output

+---------+----+----------+---------+
|    name | id |   status | user_id |
+---------+----+----------+---------+
| Norbert |  1 |   active |       1 |
|    John |  2 | inactive |       2 |
|    Jane |  3 |   active |       3 |
+---------+----+----------+---------+
3 rows

Contributors

Join us on GitHub external resource
scroll back to top