Search code examples
c#deedle

Adding two columns using Deedle in C#


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?


Solution

  • 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.