简体   繁体   English

Scala:如何在循环中合并数据帧

[英]Scala: How to do union of data frames in the loop

I would like to do union of data frame in the recursive method.我想在递归方法中进行数据框的联合。

I am doing some calculations in the recursive method and filtering the data and storing in one variable.我正在用递归方法进行一些计算并过滤数据并存储在一个变量中。 In 2nd iteration i will do some calculation and again i will store the data in same variable.when i am calling the method second time my first result is getting vanished.Ideally i have to store the result in one temp variable and i need to do union of all the result till the recursive method gets completed its execution.在第二次迭代中,我将进行一些计算,并再次将数据存储在同一个变量中。当我第二次调用该方法时,我的第一个结果消失了。理想情况下,我必须将结果存储在一个临时变量中,我需要这样做所有结果的并集,直到递归方法完成其执行。

Iteration1 output in df: df 中的迭代 1 输出:

Col1   
    14      
    35    

Iteration2 output in df: df中的迭代2输出:

Col1
    18      
    20

Now i need the final output as,现在我需要最终输出,

Col1
    14
    35
    18
    20

Code:代码:

def myRecursiveMethod(first: List[List[String]],
                        Inputcolumnsdummy: List[List[String]],
                        secondInputcolumns: List[List[String]] = {

  val ongoingResult = doSomeCalculation(first,Inputcolumnsdummy, secondInputcolumns)
}

I want my code should be something like below,我希望我的代码应该如下所示,

def myRecursiveMethod(first: List[List[String]],
                        Inputcolumnsdummy: List[List[String]],
                        secondInputcolumns: List[List[String]]) = {

    val ongoingResult = doSomeCalculation(first, Inputcolumnsdummy, secondInputcolumns)
    Val temp = temp.union(ongoingResult)
}

You should try: use union like this: df1.union(df2) or df1.union(computation(df2,...)) .你应该尝试:像这样使用uniondf1.union(df2)df1.union(computation(df2,...))

Exemple below:示例如下:

def doCompute(df: DataFrame): DataFrame = {
    val tmp: DataFrame = ... // TODO: call to your computation method
    tmp.show()
    df.union(tmp)
}

val df1: DataFrame = ...
val df2: DataFrame = ...
val df3: DataFrame = ...

var union_df: DataFrame = df1.union(doCompute(df2)).union(doCompute(df3))

One thing I did not understand in your question is how is your function myRecursiveMethod recursive?我在你的问题中不明白的一件事是你的函数myRecursiveMethod是如何递归的? A recursive function calls itself, by definition.根据定义,递归函数调用自身。 Not sure your question is really clear.不确定你的问题是否真的清楚。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM