在 C# 中使用 Deedle 添加两列

Adding two columns using Deedle in C#

给定以下 CSV 文件

A,B
2,3
5,7
9,11

我想添加两列,结果是

A,B,C
2,3,5
5,7,12
9,11,20

使用 C# 和 Deedle。

using Deedle;
using System.IO;
using System.Linq;
namespace NS
{
    class AddTwoColumns
    {
        static void main(string[] args)
        {
            var root = "path/to";
            var df = Frame.ReadCsv(Path.Combine(root, "data.csv"));

            var a = df.GetColumn<int>("A");
            var b = df.GetColumn<int>("B");
            var c = df.Select(x => x.a + x.b);
            df.AddColumn("C", c);
            df.Print();
        }
    }
}

都没有 reference 也不是教程 (series, frame) 特别有启发性。

这个简单的操作的正确df.Select()是什么?

ab 只是 Deedle.Series,您可以对其进行数值运算。所以,你可以通过添加两个系列来做到这一点:

// simply add the series
var c = a + b;
df.AddColumn("C", c);
df.Print();

// output
     A B  C
0 -> 2 3  5
1 -> 5 7  12
2 -> 9 11 20

Statistics and calculations 部分(您链接到的页面)简要提到了算术运算。它还具有关于您可能需要考虑的缺失数据的注释:

Point-wise and scalar operators automatically propagate missing data. When calculating s1 + s2 and one of the series does not contain data for a key k, then the resulting series will not contain data for k.

我知道这个问题是专门针对 C# 解决的,但我希望这种 F# 方法能以某种方式提供帮助:

Frame.ReadCsv(@"C:\Users\flavi\Downloads\sample.txt")
|> fun frame->
    Frame.addCol "C"
        (Frame.mapRowValues (fun row ->
            row.GetAs<int>("A") + row.GetAs<int>("B")
        )frame) frame