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

in spark to adding new column


+-----+------+
|EmpId|Salary|
+-----+------+
|111  |50000 |
|222  |60000 |
|333  |40000 
+-----+------+
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 :: matplotlib animate 
Python :: Determine the sum of al digits of n 
Python :: python property 
Python :: python file hashlib 
Python :: calculator in python 
Python :: python dequeu 
Python :: python initialize dict with empty list values 
Python :: Python Changing Directory 
Python :: creating empty set and append python 
Python :: what is module in python 
Python :: np append row 
Python :: generate secret key python 
Python :: python docx extract image 
Python :: python series 
Python :: square root in python 
Python :: bold some letters of string in python 
Python :: pygityb 
Python :: how to make exe from.py file 
Python :: map and filter in python 
Python :: how to convert each string to a category or int in python dataframe 
Python :: print in python 
Python :: default orange and blue matplotlib 
Python :: python find file name 
Python :: time.strftime("%H:%M:%S") in python 
Python :: plotting roc curve 
Python :: how to print upto 5 decimal places in python 
Python :: python last 3 list elements 
Python :: get keys from dictionary python 
Python :: looping through nested dictionary to nth 
Python :: add tensorflow to conda 
ADD CONTENT
Topic
Content
Source link
Name
2+5 =