Adding two columns using Deedle in C#

我是研究僧i 提交于 2020-01-24 21:45:06

问题


Given the following CSV file

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

I'd like to add the two columns, resulting in

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

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.

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


回答1:


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:

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.



来源:https://stackoverflow.com/questions/58438974/adding-two-columns-using-deedle-in-c-sharp

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!