koala

Build codecov GitHub

pandas DataFrame的简易版本。收集、访问和操作相关数据。

示例

可以从CSV文件、预先存在的列名和数据、数据表示的映射图创建DataFrame,或者创建一个空DataFrame并在之后为其提供属性。

final fromCsv = Dataframe.fromCsv(
    path: 'path/to/file.csv', 
    eolToken: '\n', 
    maxRows: 40,
    skipColumns: ['date'],
    convertDates: true,
    datePattern: 'dd-MM-yyyy'
);

final fromNamesAndData = DataFrame.fromNamesAndData(
    ['a', 'b'], 
    [
      [1, 2],
      [3, 4],
      [69, 420]
    ]
);

DataFrame类继承自包含其数据矩阵的列表,因此可以通过常规索引访问行。另一方面,可以通过调用带有包含的列名的实例来访问列。

// get a row
final secondRow = df[1];

// get a column
final bColumn = df('b');
final typedBColumn = df<double?>('b');
final slicedBColumn = df('b', start: 1, end: 5);
final filteredBColumn = df('b', includeRecord: (el) => el > 7);

// grab a singular record
final record = df<int>.record(3, 'b');

操作行和列

// add and remove rows through the built-in list methods 
df.add([2, 5]);
df.removeAt(4);
df.removeLast();

// manipulate columns
df.addColumn('newColumn', [4, 8, 2]);
df.removeColumn('newColumn');
df.transformColumn('a', (record) => record * 2);

复制或切片DataFrame

final copy = df.copy();
final sliced = df.sliced(30, 60);   
df.slice(10, 15);  // in-place counterpart

原地排序DataFrame或获取其排序副本

final sorted = df.sortedBy('a', ascending: true, nullFirst: false);
sorted.sortBy('b', ascending: false, compareRecords: (a, b) => Comparable.compare(a.toString().length, b.toString().length));

只需将DataFrame传递给print函数即可获得其可读表示

DataFrame df = DataFrame.fromRowMaps([
  {'col1': 1, 'col2': 2},
  {'col1': 1, 'col2': 1},
  {'col1': null, 'col2': 8},
]);
print(df);

输出结果为

    col1 col2
0 | 1    2   
1 | 1    1   
2 | null 8   

……等等,以此类推。

贡献

我打算积极维护这个仓库,所以欢迎提交PR,因为DataFrame还有很多功能可以添加。

致谢

这个仓库最初是从目前未维护且总体上平庸的df项目分叉出来的,但最终我重写了几乎所有内容。不过,还是得给伙计们点赞。

作者

我,w2sv

GitHub

查看 Github