2016-02-24 10 views
47

में डेटा फ्रेम के कॉलम नामों का नाम बदलना मैं स्पार्क-स्कैला में डेटाफ्रेम के सभी शीर्षलेख/कॉलम नामों को बदलने की कोशिश कर रहा हूं। अब तक मैं निम्नलिखित कोड के साथ आया हूं जो केवल एक कॉलम नाम को प्रतिस्थापित करता है। कृपया इस पर मदद करें।स्पार्क स्कैला

for(i <- 0 to origCols.length - 1){df.withColumnRenamed(df.columns(i),df.columns(i).toLowerCase);} 

उत्तर

138

तो संरचना सपाट है:

val df = Seq((1L, "a", "foo", 3.0)).toDF 
df.printSchema 
// root 
// |-- _1: long (nullable = false) 
// |-- _2: string (nullable = true) 
// |-- _3: string (nullable = true) 
// |-- _4: double (nullable = false) 

सरल बात आप कर सकते हैं toDF विधि का उपयोग करने के लिए है:

val newNames = Seq("id", "x1", "x2", "x3") 
val dfRenamed = df.toDF(newNames: _*) 

dfRenamed.printSchema 
// root 
// |-- id: long (nullable = false) 
// |-- x1: string (nullable = true) 
// |-- x2: string (nullable = true) 
// |-- x3: double (nullable = false) 

आप अलग-अलग स्तंभों आप उपयोग कर सकते हैं या तो select नाम बदलना चाहते हैं तो alias:

df.select($"_1".alias("x1")) 

जो आसानी से एकाधिक स्तंभों के लिए सामान्यीकृत किया जा सकता:

val lookup = Map("_1" -> "foo", "_3" -> "bar") 

df.select(df.columns.map(c => col(c).as(lookup.getOrElse(c, c))): _*) 

या withColumnRenamed:

df.withColumnRenamed("_1", "x1") 

जो foldLeft के साथ उपयोग एकाधिक स्तंभों नाम बदलने के लिए:

lookup.foldLeft(df)((acc, ca) => acc.withColumnRenamed(ca._1, ca._2)) 
नेस्टेड संरचनाओं के साथ

(structs) एक संभावित विकल्प का चयन करके एक संभावित विकल्प का नाम बदल रहा है ucture:

val nested = spark.read.json(sc.parallelize(Seq(
    """{"foobar": {"foo": {"bar": {"first": 1.0, "second": 2.0}}}, "id": 1}""" 
))) 

nested.printSchema 
// root 
// |-- foobar: struct (nullable = true) 
// | |-- foo: struct (nullable = true) 
// | | |-- bar: struct (nullable = true) 
// | | | |-- first: double (nullable = true) 
// | | | |-- second: double (nullable = true) 
// |-- id: long (nullable = true) 

@transient val foobarRenamed = struct(
    struct(
    struct(
     $"foobar.foo.bar.first".as("x"), $"foobar.foo.bar.first".as("y") 
    ).alias("point") 
).alias("location") 
).alias("record") 

nested.select(foobarRenamed, $"id").printSchema 
// root 
// |-- record: struct (nullable = false) 
// | |-- location: struct (nullable = false) 
// | | |-- point: struct (nullable = false) 
// | | | |-- x: double (nullable = true) 
// | | | |-- y: double (nullable = true) 
// |-- id: long (nullable = true) 

ध्यान दें कि यह nullability मेटाडेटा को प्रभावित कर सकता है।

nested.select($"foobar".cast(
    "struct<location:struct<point:struct<x:double,y:double>>>" 
).alias("record")).printSchema 

// root 
// |-- record: struct (nullable = true) 
// | |-- location: struct (nullable = true) 
// | | |-- point: struct (nullable = true) 
// | | | |-- x: double (nullable = true) 
// | | | |-- y: double (nullable = true) 

या:

import org.apache.spark.sql.types._ 

nested.select($"foobar".cast(
    StructType(Seq(
    StructField("location", StructType(Seq(
     StructField("point", StructType(Seq(
     StructField("x", DoubleType), StructField("y", DoubleType))))))))) 
).alias("record")).printSchema 

// root 
// |-- record: struct (nullable = true) 
// | |-- location: struct (nullable = true) 
// | | |-- point: struct (nullable = true) 
// | | | |-- x: double (nullable = true) 
// | | | |-- y: double (nullable = true) 
+0

हाय @ zero323 withColumnRenamed का उपयोग करते समय मैं कर रहा हूँ एक और संभावना कास्टिंग द्वारा नाम बदलने के लिए है विश्लेषण प्राप्त करना 'सीसी 8' को हल नहीं कर सकता है। 1 'दिए गए इनपुट कॉलम ... यह विफल रहता है भले ही CC8.1 डेटाफ्रेम में उपलब्ध है कृपया मार्गदर्शन करें। – u449355

+0

@ u449355 यह मेरे लिए स्पष्ट नहीं है अगर यह नेस्टेड कॉलम है या जिसमें डॉट्स हैं। बाद के मामले में बैकटिक्स काम करना चाहिए (कम से कम कुछ बुनियादी मामलों में)। – zero323

+0

'df.select' में क्या मतलब है: df.olumns.map (c => col (c) .as (lookup.getOrElse (c, c))): _ *) ' –

5

आप में से जो PySpark संस्करण में रुचि के लिए:

merchants_df_renamed = merchants_df.toDF(
    'merchant_id', 'category', 'subcategory', 'merchant') 

merchants_df_renamed.printSchema() 

root 
|-- merchant_id: integer (nullable = true) 
|-- category: string (nullable = true) 
|-- subcategory: string (nullable = true) 
|-- merchant: string (nullable = true) 
2
def aliasAllColumns(t: DataFrame, p: String = "", s: String = ""): DataFrame = 
{ 
    t.select(t.columns.map { c => t.col(c).as(p + c + s) } : _*) 
} 
संबंधित मुद्दे