如何在C#中使用Deedle仅从帧中选择包含非负值的列?

时间:2022-10-20 19:21:36

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.

1 个解决方案

#1


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:

您的代码指向正确的方向 - 您需要使用frame.Columns.Where。但是,这不会给您单独的值,而是整列(作为一个系列),因此您需要(再次)迭代列中的所有值:

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.

由于您要遍历列,因此需要在末尾调用Frame.FromColumns以将一系列列重新转换为一个帧。

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.

Where方法为列数据提供包含对象的盒装系列。使用As ()是将值作为浮点获取的有效方法。

#1


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:

您的代码指向正确的方向 - 您需要使用frame.Columns.Where。但是,这不会给您单独的值,而是整列(作为一个系列),因此您需要(再次)迭代列中的所有值:

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.

由于您要遍历列,因此需要在末尾调用Frame.FromColumns以将一系列列重新转换为一个帧。

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.

Where方法为列数据提供包含对象的盒装系列。使用As ()是将值作为浮点获取的有效方法。