Search code examples
c#deedle

How can I select only the columns containing non negative values from a frame using Deedle in C#?


I would like to select / filter the columns from a frame that do not contain negative Values. That means creating a new frame with the columns that only contain positive values.

Something like the following (syntax is wrong):

var myFrameNoNeg = myFrame.Columns.Where(kvp => kvp.Value > 0); // 

The idea is to Drop the columns which contain negative values.

Thank you.


Solution

  • Your code is pointing in the right direction - you need to use frame.Columns.Where. However, this does not give you individual values, but the whole column (as a series) and so you need to (again) iterate over all values in the column:

    using Deedle;
    using System.Linq;
    
    Frame.FromColumns(frame.Columns.Where(kvp => 
      !kvp.Value.As<double>().Values.Any(v => v < 0.0)))
    

    Since you are iterating over columns, you need to call Frame.FromColumns at the end to turn the series of columns back into a frame.

    The Where method gives you the column data as boxed series containing objects. Using As<double>() is an efficient way to get the values as floating points.