我正在使用Scala Saddle Frames和Series
Saddle系列可以转换为Frame,Saddle Frame可以用concat方法连接,如下所示:
val frame3 = frame1.concat(frame2)因为我是Scala的新手,所以我一直在尝试转换
Map[String, Series[String, Double] 至
Frame[String, String, Double] 其中帧列索引是映射键,列是映射值。
有没有人能提出一个解决方案?
发布于 2013-12-01 02:34:02
你可能已经解决了这个问题。如果没有,这里有一个似乎适合的解决方案:
object TheApp extends App {
import org.saddle.{Frame, Index, Series, Vec}
val inMap = Map(
"s1" -> Series(Vec(1.0, 2.0, 3.0, 4.0), Index("a", "b", "b", "c")),
"s2" -> Series(Vec(2.0, 3.0, 4.0, 5.0), Index("A", "B", "B", "C")),
"s3" -> Series(Vec(3.0, 4.0, 5.0, 6.8), Index("t", "C", "v", "v"))
)
val frame = inMap.foldLeft(Frame.empty[String,String,Double]) { (prevFrame, mapElement) =>
val (key, series) = mapElement
val f = Frame(key -> series)
prevFrame.concat(f)
}
println(s"Saddle Test map=$inMap frame=$frame")
}这里发生的事情是,foldLeft将每个map元素与前一次计算的结果放在一起,并调用所提供的函数来计算下一个值。
谢谢你让我意识到Saddle,我可能会发现它非常有用!
发布于 2015-04-02 06:30:27
为了便于将来参考,当索引在向量中很常见时,这是一种并排连接向量的简单方法:
import org.saddle.{Frame, Index, Series, Vec}
val ix = Index("2015-03-19", "2015-03-20", "2015-03-21", "2015-03-22", "2015-03-23")
val s1 = Series( Vec(1.0, 2.0, 3.0, 4.0, 5.0), ix)
val s2 = Series( Vec(Double.NaN, 1.0, 2.0, 3.0, 4.0), ix)
val s3 = Series( Vec(Double.NaN, Double.NaN, 1.0, 2.0, 3.0), ix)
val map = Map( "RSI" -> s1, "ADX" -> s2, "SAR" -> s3)
val frame =
map
.foldLeft(Frame.empty[String,String,Double]) {
case (prevFrame, mapElement) =>
val (key, series) = mapElement
val f = Frame(key -> series)
prevFrame.rconcat(f) }输出为:
[5 x 3]
RSI ADX SAR
------ ------ ------
2015-03-19 -> 1.0000 NA NA
2015-03-20 -> 2.0000 1.0000 NA
2015-03-21 -> 3.0000 2.0000 1.0000
2015-03-22 -> 4.0000 3.0000 2.0000
2015-03-23 -> 5.0000 4.0000 3.0000 请注意,正在使用rconcat。
https://stackoverflow.com/questions/20293014
复制相似问题