代码之家  ›  专栏  ›  技术社区  ›  dharmatech

相当于熊猫的“移位”

  •  0
  • dharmatech  · 技术社区  · 4 年前

    让我们假设在Python中有以下内容与熊猫:

    import pandas as pd
    
    df = pd.DataFrame({
        "Col1": [10, 20, 15, 30, 45],
        "Col2": [13, 23, 18, 33, 48],
        "Col3": [17, 27, 22, 37, 52] },
        index=pd.date_range("2020-01-01", "2020-01-05"))
    
    df
    

    enter image description here

    移动列

    现在让我们 shift Col1 2,并将其存储在 Col4 .

    我们还将存储 df['Col1'] / df['Col1'].shift(2) Col5 :

    df_2 = df.copy(deep=True)
    
    df_2['Col4'] = df['Col1'].shift(2)
    
    df_2['Col5'] = df['Col1'] / df['Col1'].shift(2)
    
    df_2
    

    enter image description here

    C#版本

    现在,让我们设置一个类似的 DataFrame 在C中:

    #r "nuget:Microsoft.Data.Analysis"
    
    using Microsoft.Data.Analysis;
    var df = new DataFrame(
        new PrimitiveDataFrameColumn<DateTime>("DateTime",
            Enumerable.Range(0, 5).Select(i => new DateTime(2020, 1, 1).Add(new TimeSpan(i, 0, 0, 0)))),    
        new PrimitiveDataFrameColumn<int>("Col1", new []{ 10, 20, 15, 30, 45 }),
        new PrimitiveDataFrameColumn<int>("Col2", new []{ 13, 23, 18, 33, 48 }),
        new PrimitiveDataFrameColumn<int>("Col3", new []{ 17, 27, 22, 37, 52 })
    );
    
    df
    

    结果在中。NET交互:

    enter image description here

    问题

    什么是执行Pandas版本中演示的等效列移位的好方法?

    以上示例来自以下文档: pandas.DataFrame.shift

    https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.shift.html

    使现代化

    看起来确实没有内置的 转移 Microsoft.Data.Analysis . 我在这里发布了一个问题:

    https://github.com/dotnet/machinelearning/issues/6008

    0 回复  |  直到 4 年前
        1
  •  0
  •   dharmatech    4 年前

    辅助函数

    执行列移位。

    PrimitiveDataFrameColumn<double> ShiftIntColumn(PrimitiveDataFrameColumn<int> col, int n, string name)
    {
        return 
            new PrimitiveDataFrameColumn<double>(
                name,
                Enumerable.Repeat((double?) null, n)
                    .Concat(col.Select(item => (double?) item))
                    .Take(col.Count()));
    }
    

    进行分割,照顾 null

    PrimitiveDataFrameColumn<double> DivAlt3(PrimitiveDataFrameColumn<int> a, PrimitiveDataFrameColumn<double> b, string name)
    {
        return 
            new PrimitiveDataFrameColumn<double>(name, a.Zip(b, (x, y) => y == null ? null : x / y));
    }
    

    然后是以下内容:

    var df = new DataFrame(
        new PrimitiveDataFrameColumn<DateTime>("DateTime",
            Enumerable.Range(0, 5).Select(i => 
                new DateTime(2020, 1, 1).Add(new TimeSpan(i, 0, 0, 0)))),    
        new PrimitiveDataFrameColumn<int>("Col1", new []{ 10, 20, 15, 30, 45 }),
        new PrimitiveDataFrameColumn<int>("Col2", new []{ 13, 23, 18, 33, 48 }),
        new PrimitiveDataFrameColumn<int>("Col3", new []{ 17, 27, 22, 37, 52 })
    );
    
    df.Columns.Add(ShiftIntColumn((PrimitiveDataFrameColumn<int>)df["Col1"], 2, "Col4"));
    
    df.Columns.Add(DivAlt3((PrimitiveDataFrameColumn<int>) df["Col1"], (PrimitiveDataFrameColumn<double>) df["Col4"], "Col5"));
    

    结果:

    enter image description here

    有关上述内容的完整演示,请参阅以下笔记本:

    https://github.com/dharmatech/dataframe-shift-example-cs/blob/003/dataframe-shift-example-cs.ipynb

    笔记

    • Microsoft.Data.Analysis 随附专栏 shift

    我希望看到其他可能更惯用的方法来解决这个问题。

    推荐文章