gpt4 book ai didi

f# - 重命名 Deedle 数据框列的直接功能方法

转载 作者:行者123 更新时间:2023-12-05 01:21:50 27 4
gpt4 key购买 nike

是否有一种简洁实用的方法来重命名 Deedle 数据框的列 f

f.RenameColumns(...)是可用的,但会改变它所应用的数据框,因此使重命名操作幂等有点痛苦。我有类似 f.RenameColumns (fun c -> ( if c.IndexOf( "_" ) < 0 then c else c.Substring( 0, c.IndexOf( "_" ) ) ) + "_renamed") 的东西, 这很丑。

如果能从输入帧创建一个新帧就好了,像这样:Frame( f |> Frame.cols |> Series.keys |> Seq.map someRenamingFunction, f |> Frame.cols |> Series.values )但这被第二部分绊倒了—— f |> Frame.cols |> Series.values 的类型不是 Frame 所要求的构造函数。

如何简洁地转换f |> Frame.cols |> Series.values这样它的结果就可以被 Frame 食用构造函数?

最佳答案

当与RenameColumns一起使用时,您可以确定它的功能:

df.RenameColumns someRenamingFunction

您还可以使用函数 Frame.mapColKeys。

Builds a new data frame whose columns are the results of applying the specified function on the columns of the input data frame. The function is called with the column key and object series that represents the column data. Source

例子:

type Record = {Name:string; ID:int ; Amount:int}

let data =
[|
{Name = "Joe"; ID = 51; Amount = 50};
{Name = "Tomas"; ID = 52; Amount = 100};
{Name = "Eve"; ID = 65; Amount = 20};
|]

let df = Frame.ofRecords data

let someRenamingFunction s =
sprintf "%s(%i)" s s.Length

df.Format() |> printfn "%s"

let ndf = df |> Frame.mapColKeys someRenamingFunction

ndf.Format() |> printfn "%s"

df.RenameColumns someRenamingFunction

df.Format() |> printfn "%s"

打印:

     Name  ID Amount
0 -> Joe 51 50
1 -> Tomas 52 100
2 -> Eve 65 20

Name(4) ID(2) Amount(6)
0 -> Joe 51 50
1 -> Tomas 52 100
2 -> Eve 65 20

Name(4) ID(2) Amount(6)
0 -> Joe 51 50
1 -> Tomas 52 100
2 -> Eve 65 20

关于f# - 重命名 Deedle 数据框列的直接功能方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37094516/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com