Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

spark add column to dataframe

from pyspark.sql.functions import lit

df = sqlContext.createDataFrame(
    [(1, "a", 23.0), (3, "B", -23.0)], ("x1", "x2", "x3"))

df_with_x4 = df.withColumn("x4", lit(0))
df_with_x4.show()
Comment

add column in spark dataframe


from pyspark.sql.functions import lit

df = sqlContext.createDataFrame(
    [(1, "a", 23.0), (3, "B", -23.0)], ("x1", "x2", "x3"))

df_with_x4 = df.withColumn("x4", lit(0))
df_with_x4.show()

## +---+---+-----+---+
## | x1| x2|   x3| x4|
## +---+---+-----+---+
## |  1|  a| 23.0|  0|
## |  3|  B|-23.0|  0|
## +---+---+-----+---+

Comment

add column in spark dataframe

from pyspark.sql.functions import lit

df = sqlContext.createDataFrame(
    [(1, "a", 23.0), (3, "B", -23.0)], ("x1", "x2", "x3"))

df_with_x4 = df.withColumn("x4", lit(0))
df_with_x4.show()

## +---+---+-----+---+
## | x1| x2|   x3| x4|
## +---+---+-----+---+
## |  1|  a| 23.0|  0|
## |  3|  B|-23.0|  0|
## +---+---+-----+---+
Comment

spark dataframe add column with function

val myDF = sqlContext.parquetFile("hdfs:/to/my/file.parquet")

myDF.withColumn("Code", coder(myDF("Amt")))
Comment

how to add new column in Spark

from pyspark.sql.functions import expr

# Using withColumn() method
foo2 = (foo.withColumn(
"status", expr("CASE WHEN delay <= 10 THEN 'On-time' ELSE 'Delayed' END")
))

# Output:
# +--------+-----+--------+------+-----------+-------+
# |    date|delay|distance|origin|destination| status|
# +--------+-----+--------+------+-----------+-------+
# |01010710|   31|     590|   SEA|        SFO|Delayed|
# |01010955|  104|     590|   SEA|        SFO|Delayed|
# |01010730|    5|     590|   SEA|        SFO|On-time|
# +--------+-----+--------+------+-----------+-------+
Comment

PREVIOUS NEXT
Code Example
Python :: grab a href using beuatiful soup 
Python :: print variable in string python 
Python :: remove nana from np array 
Python :: plt.savefig 
Python :: check python version conda env 
Python :: remove spaces from input python 
Python :: remove outliers numpy array 
Python :: pandas query on datetime 
Python :: how to count number of unique values in a column python 
Python :: sort defaultdict by value 
Python :: python tkinter set minimum window size 
Python :: create virtualenv in linux python 
Python :: Tkinter button icons 
Python :: remove outliers in dataframe 
Python :: ValueError: Shapes (None, 1) and (None, 11) are incompatible keras 
Python :: check string equal with regular expression python 
Python :: django modelform style 
Python :: discord.py get profile picture 
Python :: how to get column names having numeric value in pandas 
Python :: pyAudioAnalysis 
Python :: django template tags capitalize 
Python :: python test if you can convert to int 
Python :: pandas remove item from dictionary 
Python :: how to remove in null values in pandas 
Python :: get working directory in python 
Python :: python get current month 
Python :: generate gif py 
Python :: merge two dataframes with common columns 
Python :: python weekday 
Python :: instagram login with selenium py 
ADD CONTENT
Topic
Content
Source link
Name
8+8 =