dcabrio/dataframe
Composer 安装命令:
composer require dcabrio/dataframe
包简介
Archon: PHP Data Analysis Library
README 文档
README
Archon is a PHP library designed to make working with tabular/relational data, files, and databases easy. The core component of the library is the DataFrame class - a tabular data structure which raises the level of abstraction when working with tabular, two-dimensional data.
Installation
Using Composer:
composer require dcabrio/dataframe dev-master
{
"require": {
"archon/dataframe": "1.1.1"
}
}
Requirements
- PHP 7.1 or higher
- php_pdo_sqlite extension
- php_mbstring extension
Dependencies
- PHPOffice/PHPExcel: 1.8.1
- gajus/dindent: 2.0.2
License
Data Format Examples
Instantiating from an array:
$df = DataFrame::fromArray([ ['a' => 1, 'b' => 2, 'c' => 3], ['a' => 4, 'b' => 5, 'c' => 6], ['a' => 7, 'b' => 8, 'c' => 9], ]);
Reading a CSV file:
x|y|z
1|2|3
4|5|6
7|8|9
$df = DataFrame::fromCSV($fileName, [ 'sep' => '|', 'colmap' => [ 'x' => 'a', 'y' => 'b', 'z' => 'c' ] ]);
Writing a CSV file:
$df->toCSV($fileName);
"a","b","c"
"1","2","3"
"4","5","6"
"7","8","9"
Reading a fixed-width file:
foo bar baz
-----------
1 2 3
4 5 6
7 8 9
$df = DataFrame::fromFWF($fileName, [ 'a' => [0, 1], 'b' => [4, 5], 'c' => [8, 9] ], ['include' => '^[0-9]']);
Reading an XLSX spreadsheet:
$dfA = DataFrame::fromXLSX($fileName, ['sheetname' => 'Sheet A']); $dfB = DataFrame::fromXLSX($fileName, ['sheetname' => 'Sheet B']); $dfC = DataFrame::fromXLSX($fileName, ['sheetname' => 'Sheet C']);
Writing an XLSX spreadsheet:
$phpExcel = new PHPExcel(); $dfA->toXLSXWorksheet($phpExcel, 'Sheet A'); $dfB->toXLSXWorksheet($phpExcel, 'Sheet B'); $dfC->toXLSXWorksheet($phpExcel, 'Sheet C'); $writer = new PHPExcel_Writer_Excel2007($phpExcel); $writer->save($fileName);
Querying from a database:
$pdo = new PDO('sqlite::memory:'); $df = DataFrame::fromSQL('SELECT foo, bar, baz FROM table_name;', $pdo);
Committing to a database:
$pdo = new PDO('sqlite::memory:'); $affected = $df->toSQL('table_name', $pdo); echo sprintf('%d rows committed to database.', $affected);
Displaying an HTML table:
$html = $df->toHTML(['class' => 'myclass', 'id' => 'myid']);
| a | b | c |
|---|---|---|
| a | b | c |
| 1 | 2 | 3 |
| 4 | 5 | 6 |
| 7 | 8 | 9 |
With support for DataTables.js:
$dataTable = $df->toHTML(['datatable' => '{ "optionKey": "optionValue" }']);
Converting to JSON:
$json = $df->toJSON();
Creating from JSON:
$df = DataFrame::fromJSON('[ {"a": 1, "b": 2, "c": 3}, {"a": 4, "b": 5, "c": 6}, {"a": 7, "b": 8, "c": 9} ]');
Extracting the underlying two-dimensional array:
$myArray = $df->toArray(); print_r($myArray);
Array ( [0] => Array ( [a] => 1 [b] => 2 [c] => 3 ) [1] => Array ( [a] => 4 [b] => 5 [c] => 6 ) [2] => Array ( [a] => 7 [b] => 8 [c] => 9 ) )
Basic Operations
Getting column names:
$df->columns() -------------- Array ( [0] => a [1] => b [2] => c )
Adding columns:
$df['key'] = 'value';
Removing columns:
unset($df['key']);
Counting rows:
count($df);
Iterating over rows:
foreach ($df as $i => $row) { echo $i.': '.implode('-', $row).PHP_EOL; } -------------------------- 0: 1-2-3 1: 4-5-6 2: 7-8-9
Advanced Operations
Applying functions to rows:
$df = $df->apply(function ($row, $index) { $row['a'] = $row['c'] + 1; return $row; });
Applying functions to columns directly:
$df['a'] = function ($el, $key) { return $el + 3; };
Applying values to columns via function application of other columns:
$df['a'] = $df['c']->apply(function ($el, $key) { return $el + 1; });
Applying types:
$df = DataFrame::fromArray([ ['my_date' => '11/20/16'], ['my_other_date' => '2/12/2016'], ['my_decimal' => '5,000.20'], ['my_int' => '10-'], ['my_currency' => '12345.67'] ]); $df->convertTypes([ 'my_date' => 'DATE', 'my_other_date' => 'DATE', 'my_decimal' => 'DECIMAL', 'my_int' => 'INT', 'my_currency' => 'CURRENCY' ], ['m/d/y', 'd/m/Y'], 'Y-m-d'); print_r($df->toArray());
Array ( [0] => Array ( [my_date] => '2016-11-20' [my_other_date] => '2016-12-2' [my_decimal] => '5000.20' [my_int] => '-10' [my_currency] => '$12,345.67' ) )
Manipulating DataFrame using SQL:
$df = DataFrame::fromArray([ ['a' => 1, 'b' => 2, 'c' => 3], ['a' => 4, 'b' => 5, 'c' => 6], ['a' => 7, 'b' => 8, 'c' => 9], ]); $df = $df->query(" SELECT a, b FROM dataframe WHERE a = '4' OR b = '2'; "); print_r($df->toArray());
Array ( [0] => Array ( [a] => 1 [b] => 2 ) [1] => Array ( [a] => 4 [b] => 5 ) )
$df = DataFrame::fromArray([ ['a' => 1, 'b' => 2, 'c' => 3], ['a' => 4, 'b' => 5, 'c' => 6], ['a' => 7, 'b' => 8, 'c' => 9], ]); $df = $df->query(" UPDATE dataframe SET a = c * 2; "); print_r($df['a']->to_array());
Array ( [0] => Array ( [a] => 6 ) [1] => Array ( [a] => 12 ) [2] => Array ( [a] => 18 ) )
dcabrio/dataframe 适用场景与选型建议
dcabrio/dataframe 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 74 次下载、GitHub Stars 达 0, 最近一次更新时间为 2022 年 09 月 29 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「database」 「sql」 「data」 「excel」 「csv」 「data analysis」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 dcabrio/dataframe 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 dcabrio/dataframe 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 dcabrio/dataframe 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Shoot aims to make providing data to your templates more manageable
Dibi is Database Abstraction Library for PHP
Store your language lines in the database, yaml or other sources
Adds the EDTF data type to Wikibase
A package for automatically encrypting and decrypting Eloquent attributes in Laravel 5.5+, based on configuration settings.
A simple library that allows transform any kind of data to native php data or whatever
统计信息
- 总下载量: 74
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 23
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: BSD-3-Clause
- 更新时间: 2022-09-29