如何用另一个数据帧头更改一个数据帧的头? [英] how to change header of a data frame with another data frame header?

查看:114
本文介绍了如何用另一个数据帧头更改一个数据帧的头?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个看起来像这样的数据集

I have a data set which looks like this

LineItem.organizationId|^|LineItem.lineItemId|^|StatementTypeCode|^|LineItemName|^|LocalLanguageLabel|^|FinancialConceptLocal|^|FinancialConceptGlobal|^|IsDimensional|^|InstrumentId|^|LineItemSequence|^|PhysicalMeasureId|^|FinancialConceptCodeGlobalSecondary|^|IsRangeAllowed|^|IsSegmentedByOrigin|^|SegmentGroupDescription|^|SegmentChildDescription|^|SegmentChildLocalLanguageLabel|^|LocalLanguageLabel.languageId|^|LineItemName.languageId|^|SegmentChildDescription.languageId|^|SegmentChildLocalLanguageLabel.languageId|^|SegmentGroupDescription.languageId|^|SegmentMultipleFundbDescription|^|SegmentMultipleFundbDescription.languageId|^|IsCredit|^|FinancialConceptLocalId|^|FinancialConceptGlobalId|^|FinancialConceptCodeGlobalSecondaryId|^|FFAction|!|
Japan|^|1507101869432|^|4295876606|^|1|^|BAL|^|Cash And Deposits|^|null|^|null|^|ACAE|^|false|^|null|^|null|^|null|^|null|^|false|^|null|^|null|^|null|^|null|^|505126|^|505074|^|null|^|null|^|null|^|null|^|null|^|null|^|null|^|3018759|^|null|^|I|!|

这就是我如何使用自动发现模式加载数据

And this is how i load data with auto discover schema

val df1With_ = df.toDF(df.columns.map(_.replace(".", "_")): _*)
val column_to_keep = df1With_.columns.filter(v => (!v.contains("^") && !v.contains("!") && !v.contains("_c"))).toSeq
val df1result = df1With_.select(column_to_keep.head, column_to_keep.tail: _*)

现在我有另一个数据框,可以在该数据框上进行联接操作,最后创建一个数据框,将输出内容写入csv文件.

Now i have another data frame on which i do join operation and finally i create a data frame which writes output to csv file .

最终数据框如下所示

val dfMainOutputFinal = dfMainOutput.select($"DataPartition", $"StatementTypeCode",concat_ws("|^|", dfMainOutput.schema.fieldNames.filter(_ != "DataPartition").map(c => col(c)): _*).as("concatenated"))

val dfMainOutputFinalWithoutNull = dfMainOutputFinal.withColumn("concatenated", regexp_replace(col("concatenated"), "null", ""))

dfMainOutputFinalWithoutNull.write.partitionBy("DataPartition","StatementTypeCode")
  .format("csv")
  .option("nullValue", "")
  .option("header","true")
  .option("codec", "gzip")
  .save("output")

现在在输出文件中,我只看到标题为concatenated,这是预期的.

Now in my output file i see my header as only concatenated which is expected .

现在我的问题是无论如何都将最终输出的标头更改为df1result数据帧的标头

Now my question is is there anyway to change header of my final output as header of df1result data frame

推荐答案

我认为解决此问题的最简单方法是重命名concatenated列.由于列名称已经存在于column_to_keep变量中,因此您只需执行以下操作即可:

I believe the simplest way to solve this would be to rename the concatenated column. As the column names already exists in the column_to_keep variable, you can simply do:

val header = column_to_keep.mkString("|^|")
val dfMainOutputFinalWithoutNull = dfMainOutputFinal
  .withColumn("concatenated", regexp_replace(col("concatenated"), "null", ""))
  .withColumnRenamed("concatenated", header)

这将导致列名过长,因此,如果将其保存到csv之外,我不建议这样做.

This will result is an extremely long column name, hence, I wouldn't advice it if it was for something else than saving to a csv.

这篇关于如何用另一个数据帧头更改一个数据帧的头?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆