Vrokipal
Vrokipal

Reputation: 824

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?

Upvotes: 1

Views: 870

Answers (2)

FRocha
FRocha

Reputation: 960

I know this question is particularly addressed for C#, but I hope this F# approach can help somehow:

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

Upvotes: 0

haldo
haldo

Reputation: 16711

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.

Upvotes: 3

Related Questions