How to add a persistent column to a Spark DataFrame?

I want to add a column to a DataFrame with some arbitrary value (same for each row). I get an error when I use withColumn as follows:

 dt.withColumn('new_column', 10).head(5) --------------------------------------------------------------------------- AttributeError Traceback (most recent call last) <ipython-input-50-a6d0257ca2be> in <module>() 1 dt = (messages 2 .select(messages.fromuserid, messages.messagetype, floor(messages.datetime/(1000*60*5)).alias("dt"))) ----> 3 dt.withColumn('new_column', 10).head(5) /Users/evanzamir/spark-1.4.1/python/pyspark/sql/dataframe.pyc in withColumn(self, colName, col) 1166 [Row(age=2, name=u'Alice', age2=4), Row(age=5, name=u'Bob', age2=7)] 1167 """ -> 1168 return self.select('*', col.alias(colName)) 1169 1170 @ignore_unicode_prefix AttributeError: 'int' object has no attribute 'alias' 

It seems that I can trick the function into working as I want by adding and subtracting one of the other columns (so they add to zero), and then adding the number I want (10 in this case):

 dt.withColumn('new_column', dt.messagetype - dt.messagetype + 10).head(5) [Row(fromuserid=425, messagetype=1, dt=4809600.0, new_column=10), Row(fromuserid=47019141, messagetype=1, dt=4809600.0, new_column=10), Row(fromuserid=49746356, messagetype=1, dt=4809600.0, new_column=10), Row(fromuserid=93506471, messagetype=1, dt=4809600.0, new_column=10), Row(fromuserid=80488242, messagetype=1, dt=4809600.0, new_column=10)] 

This is highly hacker, isn't it? I assume there is a more legitimate way to do this?

+73
python dataframe apache-spark pyspark spark-dataframe
Sep 25 '15 at 18:17
source share
3 answers

Spark 2.2 +

Spark 2.2 introduces typedLit to support Seq , Map and Tuples ( SPARK-19254 ), and the following calls (Scala) must be supported:

 import org.apache.spark.sql.functions.typedLit df.withColumn("some_array", typedLit(Seq(1, 2, 3))) df.withColumn("some_struct", typedLit(("foo", 1, .0.3))) df.withColumn("some_map", typedLit(Map("key1" -> 1, "key2" -> 2))) 

Spark 1. 3+ ( lit ), 1. 4+ ( array , struct ), 2. 0+ ( map ):

The second argument to DataFrame.withColumn should be Column so you should use a literal:

 from pyspark.sql.functions import lit df.withColumn('new_column', lit(10)) 

If you need complex columns, you can build them using blocks like array :

 from pyspark.sql.functions import array, create_map, struct df.withColumn("some_array", array(lit(1), lit(2), lit(3))) df.withColumn("some_struct", struct(lit("foo"), lit(1), lit(.3))) df.withColumn("some_map", create_map(lit("key1"), lit(1), lit("key2"), lit(2))) 

Exactly the same methods can be used in Scala.

 import org.apache.spark.sql.functions.{array, lit, map, struct} df.withColumn("new_column", lit(10)) df.withColumn("map", map(lit("key1"), lit(1), lit("key2"), lit(2))) 

To provide names for structs use any alias in each field:

 df.withColumn( "some_struct", struct(lit("foo").alias("x"), lit(1).alias("y"), lit(0.3).alias("z")) ) 

or cast on the whole object

 df.withColumn( "some_struct", struct(lit("foo"), lit(1), lit(0.3)).cast("struct<x: string, y: integer, z: double>") ) 

It is also possible, albeit slower, to use UDF.

Note :

The same constructs can be used to pass constant arguments to UDF or SQL functions.

+141
Sep 25 '15 at 18:40
source share

In spark 2.2, there are two ways to add a constant value to a column in a DataFrame:

1) Using lit

2) Using typedLit .

The difference between the two is that typedLit can also handle parameterized scala types, such as List, Seq, and Map.

Example DataFrame:

 val df = spark.createDataFrame(Seq((0,"a"),(1,"b"),(2,"c"))).toDF("id", "col1") +---+----+ | id|col1| +---+----+ | 0| a| | 1| b| +---+----+ 

1) Using lit : Adding a constant string value to a new column named newcol:

 import org.apache.spark.sql.functions.lit val newdf = df.withColumn("newcol",lit("myval")) 

Result:

 +---+----+------+ | id|col1|newcol| +---+----+------+ | 0| a| myval| | 1| b| myval| +---+----+------+ 

2) Using typedLit :

 import org.apache.spark.sql.functions.typedLit df.withColumn("newcol", typedLit(("sample", 10, .044))) 

Result:

 +---+----+-----------------+ | id|col1| newcol| +---+----+-----------------+ | 0| a|[sample,10,0.044]| | 1| b|[sample,10,0.044]| | 2| c|[sample,10,0.044]| +---+----+-----------------+ 
+9
Feb 26 '18 at 9:05
source share
 class Insertid: ''' Example usage: gen_id=Insertid() df=genid(model_data) ''' def __init__(self, ColName='*', IDColName='id'): self.ColName = ColName self.IDColName = IDColName self.index_dict = {'index': -1} def increaingId(self, row): row = row.asDict() self.index_dict['index'] += 1 row['index'] = self.index_dict['index'] return row def fit(self, df): df = df.select(*self.ColName.split(',')) self.df = df.rdd.map(lambda row: self.increaingId(row)).toDF() def __call__(self, df): self.fit(df) return self.df 

spark: 2.0.2 id: 0,1, ... n

0
Dec 14 '18 at 9:35
source share



All Articles