在C#中使用Deedle添加两列 [英] Adding two columns using Deedle in C#

查看:332
本文介绍了在C#中使用Deedle添加两列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

提供以下CSV文件

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

我想添加两列,结果

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

使用C#和Deedle.

using C# and 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();
        }
    }
}

参考 也不是本教程 (系列框架) 特别具有启发性.

Neither the reference nor the tutorial (series, frame) is particularly illuminating.

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

What is the correct df.Select() for this simple operation?

推荐答案

ab只是Deedle.Series,可以对其执行数字运算.因此,只需添加两个系列即可完成此操作:

a and b are just Deedle.Series which you can perform numerical operations on. So, you can do this just by adding both 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

该页面的统计和计算部分您链接到的内容)简要提到了算术运算.它还提供了有关丢失数据的注释,您可能需要考虑以下问题:

The Statistics and calculations section (of the page you linked to) briefly mentions arithmetic operations. It also features a note on missing data which you might need to consider:

逐点和标量运算符会自动传播丢失的数据. 计算s1 + s2时,该系列之一不包含数据 对于键k,则结果系列将不包含k的数据.

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#中使用Deedle添加两列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆