add header and column to dataframe spark

狂风中的少年 提交于 2019-12-10 18:22:10

问题


hi guys i've got a dataframe on which i want to add a header and a first column manually here is the dataframe

import org.apache.spark.sql.SparkSession 

val spark = SparkSession.builder.master("local").appName("my-spark-app").getOrCreate()
val df = spark.read.option("header",true).option("inferSchema",true).csv("C:\\gg.csv").cache()

the content of the dataframe

12,13,14
11,10,5
3,2,45

the expected output is

define,col1,col2,col3
c1,12,13,14
c2,11,10,5
c3,3,2,45

Any help would be appreciated.


回答1:


What you want to do is:

df.withColumn("columnName", column) //here "columnName" should be "define" for you

Now you just need to create the said column (this might help)




回答2:


Here is a solution that depends on Spark 2.4:

import org.apache.spark.sql.SparkSession
import org.apache.spark.sql.types.{IntegerType, StringType, StructField, StructType}
import org.apache.spark.sql.Row

//First off the dataframe needs to be loaded with the expected schema

val spark = SparkSession.builder().appName().getOrCreate()

val schema = new StructType()
              .add("col1",IntegerType,true)
              .add("col2",IntegerType,true)
              .add("col3",IntegerType,true)

val df = spark.read.format("csv").schema(schema).load("C:\\gg.csv").cache()

val rddWithId = df.rdd.zipWithIndex

// Prepend "define" column of type Long
val newSchema = StructType(Array(StructField("define", StringType, false))         ++ df.schema.fields)

val dfZippedWithId =  spark.createDataFrame(rddWithId.map{ 
                       case (row, index) => 
                       Row.fromSeq(Array("c" + index) ++ row.toSeq)}, newSchema)
// Show results
dfZippedWithId.show

Displays:

+------+----+----+----+
|define|col1|col2|col3|
+------+----+----+----+
|    c0|  12|  13|  14|
|    c1|  11|  10|   5|
|    c2|   3|   2|  45|
+------+----+----+----+

This is a mix of the documentation here and this example.



来源:https://stackoverflow.com/questions/43141530/add-header-and-column-to-dataframe-spark

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!