F# 基于行映射在Deedle中返回多列/a数据帧
我希望查看框架中的每一行,并基于该行中的值为新框架构造多个列 最终结果应该是一个包含原始框架的列和新列的框架F# 基于行映射在Deedle中返回多列/a数据帧,f#,deedle,F#,Deedle,我希望查看框架中的每一行,并基于该行中的值为新框架构造多个列 最终结果应该是一个包含原始框架的列和新列的框架 titanic?test <- titanic |> Frame.mapRowValues( fun x -> if x.GetAs<int>("Pclass") > 1 then dict ["A", 1; "B", 2] else dict ["A", 2 ; &quo
titanic?test <- titanic |> Frame.mapRowValues( fun x -> if x.GetAs<int>("Pclass") > 1 then dict ["A", 1; "B", 2] else dict ["A", 2 ; "B", 1] );;
titanic |> Frame.expandCols ["test"];;
我有一个解决办法,但不知是否有更好的办法。我认为最好的解释方式是用一个例子。我正在使用:
这是该框架的外观:
val titanic : Frame<int,string> =
PassengerId Survived Pclass Name Sex Age SibSp Parch Ticket Fare Cabin Embarked
0 -> 1 False 3 Braund, Mr. Owen Harris male 22 1 0 A/5 21171 7.25 S
1 -> 2 True 1 Cumings, Mrs. John Bradley (Florence Briggs Thayer) female 38 1 0 PC 17599 71.2833 C85 C
注意最后两列是test.A和test.B。这种方法有效地创建了一个新的帧(a和B),然后将该帧连接到现有帧
这对于我的用例来说是很好的,但是对于其他人来说可能会感到困惑。此外,它还强制在最后的列上添加前缀,例如“test”,这不是很理想的
有没有办法将新值附加到上面代码中x表示的行序列的末尾?我发现您的方法非常优雅和聪明。因为新系列与原始框架共享索引,所以它也将非常快。因此,我认为您的解决方案实际上可能比备选方案更好(但我没有衡量这一点) 无论如何,另一个选项是从
Frame.mapRowValues
call返回新行-因此对于每一行,我们将返回原始行和附加列
titanic
|> Frame.mapRowValues(fun x ->
let add =
if x.GetAs<int>("Pclass") > 1 then series ["A", box 1; "B", box 2]
else series ["A", box 2 ; "B", box 1]
Series.merge x add)
|> Frame.ofRows
泰坦尼克号
|>Frame.mapRowValues(有趣的x->
让我们添加=
如果x.GetAs(“Pclass”)>1,则系列[“A”,方框1;“B”,方框2]
else系列[“A”,方框2;“B”,方框1]
Series.merge x(添加)
|>路框
PassengerId Survived Pclass Name Sex Age SibSp Parch Ticket Fare Cabin Embarked test.A test.B
0 -> 1 False 3 Braund, Mr. Owen Harris male 22 1 0 A/5 21171 7.25 S 1 2
1 -> 2 True 1 Cumings, Mrs. John Bradley (Florence Briggs Thayer) female 38 1 0 PC 17599 71.2833 C85 C 2 1
titanic
|> Frame.mapRowValues(fun x ->
let add =
if x.GetAs<int>("Pclass") > 1 then series ["A", box 1; "B", box 2]
else series ["A", box 2 ; "B", box 1]
Series.merge x add)
|> Frame.ofRows