在 C# 中使用 Deedle 连接两个字符串列

Vro*_*pal 3 c# deedle

该函数add_ints 正确添加两个整数列

A,B
2,3
5,7
9,11
Run Code Online (Sandbox Code Playgroud)

在 CSV 文件中。

为什么函数add_strings不能正确连接两个字符串列

L,R
"a","b"
"c","d"
"e","f"
Run Code Online (Sandbox Code Playgroud)

进入第三列

L,R,C
"a","b","ab"
"c","d","cd"
"e","f","ef"
Run Code Online (Sandbox Code Playgroud)

从类似的 CSV 文件开始时?

using Deedle;
using System.IO;

namespace NS
{
    class TwoColumnOps
    {
        static void Main(string[] args)
        {
            string root = "path/to";
            add_ints(root);
            add_strings(root);
        }
        static void add_ints(string root)
        {
            Deedle.Frame<int, string> df = Frame.ReadCsv(Path.Combine(root, "data_ints.csv"));

            Series<int, int> a = df.GetColumn<int>("A");
            Series<int, int> b = df.GetColumn<int>("B");

            Series<int, int> c = a + b;
            df.AddColumn("C", c);
            df.Print();
        }
        static void add_strings(string root)
        {
            Deedle.Frame<int, string> df = Frame.ReadCsv(Path.Combine(root, "data_strings.csv"));

            Series<int, string> a = df.GetColumn<string>("L");
            Series<int, string> b = df.GetColumn<string>("R");

            // Series<int, string> c = a + b;
            // Series<int, string> c = $"{a} and {b}";
            Series<int, string> c = string.Concat(a, b);

            df.AddColumn("C", c);
            df.Print();
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

所有三种串联方式的错误是:

Error   CS0029  Cannot implicitly convert type 'string' to 'Deedle.Series<int, string>' 
Run Code Online (Sandbox Code Playgroud)

Tom*_*cek 5

之所以+适用于数字系列,但string.Concat不适用于字符串系列,是因为系列类型+为数字系列定义了一个重载运算符。遗憾的是,这仅适用于数字。

对于非数字系列,最简单的选择是使用ZipInner对齐两个系列。这为您提供了一系列元组。然后,您可以使用Select以元素方式转换值:

var df = Frame.ReadCsv("/some/test/file.csv");
var s1 = df.GetColumn<string>("first");
var s2 = df.GetColumn<string>("second");
var added = s1.ZipInner(s2).Select(t => t.Value.Item1 + t.Value.Item2);
df.AddColumn("added", added);
Run Code Online (Sandbox Code Playgroud)