小编Jer*_*ezo的帖子

Pyspark 自动重命名重复列

我想自动重命名 df 的重复列。例如:

df 
Out[4]: DataFrame[norep1: string, num1: string, num1: bigint, norep2: bigint, num1: bigint, norep3: bigint]
Run Code Online (Sandbox Code Playgroud)

应用一些函数以 df 结尾,例如:

f_rename_repcol(df) 
Out[4]: DataFrame[norep1: string, num1_1: string, num1_2: bigint, norep2: bigint, num1_3: bigint, norep3: bigint]
Run Code Online (Sandbox Code Playgroud)

我已经创建了自己的函数并且可以工作,但我确信有一种更短、更好的方法来实现它:

def f_df_col_renombra_rep(df):
    from collections import Counter
    from itertools import chain
    import pandas as pd

    columnas_original = np.array(df.columns)
    d1 = Counter(df.columns)
    i_corrige = [a>1 for a in dict(d1.items()).values()]
    var_corrige = np.array(dict(d1.items()).keys())[i_corrige]
    var_corrige_2 = [a for a in columnas_original if a in var_corrige]
    columnas_nuevas = []
    for …
Run Code Online (Sandbox Code Playgroud)

rename multiple-columns apache-spark-sql pyspark

5
推荐指数
1
解决办法
3304
查看次数

Spark 将新的拟合阶段添加到现有 PipelineModel 中,无需再次拟合

我有一个保存的 PipelineModel:

pipe_model = pipe.fit(df_train)
pipe_model.write().overwrite().save("/user/pipe_text_2")
Run Code Online (Sandbox Code Playgroud)

现在我想向此管道添加一个新的已安装的 PipelineModel:

pipe_model = PipelineModel.load("/user/pipe_text_2")
df2 = pipe_model.transform(df1)

kmeans = KMeans(k=20)
pipe2 = Pipeline(stages=[kmeans])
pipe_model2 = pipe2.fit(df2)
Run Code Online (Sandbox Code Playgroud)

不用重新装也可以吗?为了获得一个新的PipelineModel但不是一个新的Pipeline。理想的情况如下:

pipe_model_new = pipe_model + pipe_model2
TypeError: unsupported operand type(s) for +: 'PipelineModel' and 'PipelineModel'
Run Code Online (Sandbox Code Playgroud)

我发现将两个 Spark mllib 管道连接在一起,但是使用此解决方案,您需要再次安装整个管道。这就是我试图避免的。

pipeline apache-spark pyspark apache-spark-mllib

4
推荐指数
1
解决办法
2190
查看次数