Simple DataFrames library

Aravinda VK mail at aravindavk.in
Tue Oct 29 16:11:08 UTC 2024


Hello everyone,

I am happy to share my DataFrame library for D. My primary focus 
was to make it simple to use and I haven't spent a lot of time 
optimizing the code for memory and performance.

Example:

```d
import std.stdio;
import std.algorithm;
import std.array;
import std.range;

import dataframes;

struct Product
{
     string name;
     double unitPrice;
     int quantity;
     double discount;
     double totalPrice;
}

const DISCOUNTS = [
     "WELCOME": 5,
     "HAPPY": 2
];

double[] applyDiscounts(Column!double values, string coupon = "")
{
     auto pct = coupon in DISCOUNTS;
     if (pct is null)
         return iota(values.length).map!("0.0").array;

     return values.map!(v => v * (*pct/100.0)).array;
}

void main(string[] args)
{
     auto coupon = args.length > 1 ? args[1] : "";
     auto df = new DataFrame!Product(
         name: ["p1", "p2", "p3", "p4"],
         unitPrice: [10.0, 15.0, 5.0, 20.0],
         quantity: [3, 1, 5, 2]
     );

     df.discount = df.unitPrice.applyDiscounts(coupon);
     df.totalPrice = (df.unitPrice - df.discount) * df.quantity;

     // Preview
     df.writeln;

     auto total = df.rows
         .map!(r => r.totalPrice)
         .sum;

     writeln("Total: ", total);
}
```

Highlights:

- Creates a new Class with all the fields of the given struct as 
arrays.
- Supports column operations like adding two columns or 
multiplying each values of the elements etc.
- `df.rows` will return the list of `Row` with only reference to 
the main data.
- Easy to use with `std.algorithm` goodies (Refer README).

Add `dataframes` to your project by running,

```
dub add dataframes
```

The code and the documentation are available on GitHub 
https://github.com/aravindavk/dataframes-d and 
https://code.dlang.org/packages/dataframes

Please feel free to use it and let me know your experience and 
suggestions.

Thanks
Aravinda



More information about the Digitalmars-d-announce mailing list