Search code examples
c#framecalculated-columnsdeedle

C# Deedle Dataframe compare columns to create new resulting column


Someone put me out of my misery please! I can't see the forest for all those trees. I have a populated Deedle frame which is effectively of type:

Frame<int, string> MyPopulatedFrame

I won't bore you with the initial population routine, but it prints out fine with "value1" and "value2" columns, no issues.

value1, value2
1,2
10,5
100,200
etc etc

I can then add this code to sum the two columns, which also works fine over the whole series:

MyPopulatedFrame.AddColumn("value1add2", PairOHLCVFrame.GetColumn<double>("value1") + PairOHLCVFrame.GetColumn<double>("value2"));

It gives me a delightful little calculated column on the end of my frame with the correct value in each of the rows. i.e.

value1, value2, value1add2
1,2,3
10,5,15
100,200,300
etc etc

What I would really like to do is populate another column with a 1 or 0 dependent upon value1 being greater than value2. The code ought to look something like this:

MyPopulatedFrame.AddColumn("isValue1GreaterThanValue2", PairOHLCVFrame.GetColumn<double>("value1") > PairOHLCVFrame.GetColumn<double>("value2") ? 1 : 0);

but that won't even compile. Giving a design time error of:

"Operator '>' cannot be applied to operands of type 'Series<int, double>' and 'Series<int, double>'"

I know the answer to this must be simple, but it's a case of getting there before my laptop lands in the garden...


Solution

  • If you are populating from a collection you can simply compare value1 and value2 and store the result in a series.

     var comparedResult = objects.Select(x => x.Value1 > x.Value2? 1:0).ToOrdinalSeries();
        MyPopulatedFrame.AddColumn("IsGreater", comparedResult);
    

    If you have need to get value columns from another dataframe then try the below one.

      var newList = dfObjects.Rows.Select(x => new { Value1 = x.Value.GetAs<int>("Value1"),
                       Value2 = x.Value.GetAs<int>("Value2")
                   }).Observations;
                    var comparedResult = newList.Select(x => x.Value.Value1 >= x.Value.Value2 ? 1 : 0).ToOrdinalSeries();
                    dfObjects.AddColumn("IsGreater", comparedResult);