如何使用Deedle(帧输入和帧输出)执行基于行的流程

本文关键字:于行 执行 输出 Deedle 何使用 输入 | 更新日期: 2023-09-27 18:24:23

我正在尝试使用Deedle在DataFrame上执行基于行的过程。但我就是无法让自己的思维适应Deedle的方式。

比如说像这样的帧

    Indicator1 Indicator2
1   100        200
2   300        500
3   -200       1000

假设有一些规则需要应用于每个指标:

  1. 如果指标值小于500且大于0,则将其乘以1.1
  2. 如果Indicator值小于0,则将其设为NaN

我一直在尝试使用Frame.mapRow。。。。功能。

我知道我可以使用

fun v -> let indVal = v.GetAs<Int>("Indicator1");
         let newIndVal = match indVal with 
                         |...... logic  
                         |...... some other logic
         let indVal2 = v.GetAs<Int>("Indicator2");
         let newIndVal2 = match indVal2 with 
                         |...... logic  
                         |...... some other logic  

Frame.mapRow。。。。

但我一直纠结于如何使newIndValnewIndVal2返回到一行中,并最终返回到一个新的数据帧中。

我正在努力实现的是一个框架。此外,我只知道一个接一个地处理列(在通过索引或名称检索它们之后)。如果要应用的逻辑是通用的,有没有办法不逐列应用逻辑?

使用C或C#2d数组实现这一点的一种必要(而且非常简单)的方法是

loop through the row dimension
    loop through the column dimension
         apply the rule as the side effect to the array[row,col]

如何在Deedle中实现这一点?

更新:

如果计算不需要引用同一行中的其他列,那么Leaf Garland的建议非常有效。对于我的情况,我需要逐行查看数据,因此我想使用Frame.mapRows。我应该清楚简化的要求:

比如说像这样的帧

    Indicator1 Indicator2
1   100        200
2   <Missing>  500
3   -200       1000
4   100        <Missing>
5   <Missing>  500
6   -200       100

例如如果指示器1小于300,则新的指示器2值为指示器2+5%*指示器1

我需要使用

mapRows fun k v -> let var1 = v.get("Indicator1")
                   let var2 = v.get("Indicator2")
                   run through the conditions and produce new var1 and var2
                   produce a objectSeries
|> Frame.ofRows

上面的pesudo代码听起来很简单,但我只知道如何重现一个合适的objectSeries来重现Frame。

我还注意到了一些我无法用mapRows函数解释的事情[SO问题]:Deedle Frame.mapRows如何正确使用它以及如何正确构建objectseries

更新

自从最初的问题发布后,我就在C#中使用了Deedle。令我惊讶的是,基于行的计算在C#中非常容易,并且C#Frame.rows函数处理缺失值的方式与F#mapRows函数非常不同。下面是一个非常基本的例子,我用它来验证逻辑。它可能对任何正在搜索类似应用程序的人都有用:

需要注意的事项有:1.rows函数没有删除该行,因为两列的值都丢失了2.均值函数足够聪明,可以根据可用数据点计算均值。

using System.Text;
using System.Threading.Tasks;
using Deedle;
namespace TestDeedleRowProcessWithMissingValues
{
    class Program
    {
        static void Main(string[] args)
        {
            var s1 = new SeriesBuilder<DateTime, double>(){
                 {DateTime.Today.Date.AddDays(-5),10.0},
                 {DateTime.Today.Date.AddDays(-4),9.0},
                 {DateTime.Today.Date.AddDays(-3),8.0},
                 {DateTime.Today.Date.AddDays(-2),double.NaN},
                 {DateTime.Today.Date.AddDays(-1),6.0},
                 {DateTime.Today.Date.AddDays(-0),5.0}
             }.Series;
            var s2 = new SeriesBuilder<DateTime, double>(){
                 {DateTime.Today.Date.AddDays(-5),10.0},
                 {DateTime.Today.Date.AddDays(-4),double.NaN},
                 {DateTime.Today.Date.AddDays(-3),8.0},
                 {DateTime.Today.Date.AddDays(-2),double.NaN},
                 {DateTime.Today.Date.AddDays(-1),6.0}                 
             }.Series;
            var f = Frame.FromColumns(new KeyValuePair<string, Series<DateTime, double>>[] { 
                KeyValue.Create("s1",s1),
                KeyValue.Create("s2",s2)
            });
            s1.Print();
            f.Print();

            f.Rows.Select(kvp => kvp.Value).Print();
//            29/05/2015 12:00:00 AM -> series [ s1 => 10; s2 => 10]
//            30/05/2015 12:00:00 AM -> series [ s1 => 9; s2 => <missing>]
//            31/05/2015 12:00:00 AM -> series [ s1 => 8; s2 => 8]
//            1/06/2015 12:00:00 AM  -> series [ s1 => <missing>; s2 => <missing>]
//            2/06/2015 12:00:00 AM  -> series [ s1 => 6; s2 => 6]
//            3/06/2015 12:00:00 AM  -> series [ s1 => 5; s2 => <missing>]

            f.Rows.Select(kvp => kvp.Value.As<double>().Mean()).Print();
//            29/05/2015 12:00:00 AM -> 10
//            30/05/2015 12:00:00 AM -> 9
//            31/05/2015 12:00:00 AM -> 8
//            1/06/2015 12:00:00 AM  -> <missing>
//            2/06/2015 12:00:00 AM  -> 6
//            3/06/2015 12:00:00 AM  -> 5

            //Console.ReadLine();
        }
    }
}

如何使用Deedle(帧输入和帧输出)执行基于行的流程

您可以使用Frame.mapValues映射帧中的所有值。为它提供一个函数,该函数接受您的数据类型并返回更新后的值。

let indicator1 = [100.0;300.0;-200.0] |> Series.ofValues
let indicator2 = [200.0;500.0;1000.0] |> Series.ofValues
let frame = Frame.ofColumns ["indicator1" => indicator1; "indicator2" => indicator2]
// val frame : Frame<int,string> =
// 
//     indicator1 indicator2
// 0 -> 100        200       
// 1 -> 300        500       
// 2 -> -200       1000     
let update v =
  match v with
  |v when v<500.0 && v>0.0 -> v * 1.1
  |v when v<0.0 -> nan
  |v -> v
let newFrame = frame |> Frame.mapValues update
// val newFrame : Frame<int,string> =
//  
//      indicator1 indicator2
// 0 -> 110        220       
// 1 -> 330        500       
// 2 -> <missing>  1000