Niladri Basu
Niladri Basu

Reputation: 10614

How to pass schema to create a new Dataframe from existing Dataframe?

To pass schema to a json file we do this:

from pyspark.sql.types import (StructField, StringType, StructType, IntegerType)
data_schema = [StructField('age', IntegerType(), True), StructField('name', StringType(), True)]
final_struc = StructType(fields = data_schema)
df =spark.read.json('people.json', schema=final_struc)

The above code works as expected. However now, I have data in table which I display by:

df = sqlContext.sql("SELECT * FROM people_json")               

But if I try to pass a new schema to it by using following command it does not work.

df2 = spark.sql("SELECT * FROM people_json", schema=final_struc)

It gives the following error:

sql() got an unexpected keyword argument 'schema'

NOTE: I am using Databrics Community Edition

Upvotes: 16

Views: 54467

Answers (2)

bhargav3vedi
bhargav3vedi

Reputation: 619

There is already one answer available but still I want to add something.

  1. Create DF from RDD
  • using toDF

    newDf = rdd.toDF(schema, column_name_list)

  • using createDataFrame

    newDF = spark.createDataFrame(rdd ,schema, [list_of_column_name])

  1. Create DF from other DF

suppose I have DataFrame with columns|data type - name|string, marks|string, gender|string.

if I want to get only marks as integer.

newDF = oldDF.select("marks")
newDF_with_int = newDF.withColumn("marks", df['marks'].cast('Integer'))

This will convert marks to integer.

Upvotes: 1

koiralo
koiralo

Reputation: 23109

You cannot apply a new schema to already created dataframe. However, you can change the schema of each column by casting to another datatype as below.

df.withColumn("column_name", $"column_name".cast("new_datatype"))

If you need to apply a new schema, you need to convert to RDD and create a new dataframe again as below

df = sqlContext.sql("SELECT * FROM people_json")
val newDF = spark.createDataFrame(df.rdd, schema=schema)

Hope this helps!

Upvotes: 23

Related Questions