📜  在 PySpark DataFrame 中添加具有字面量值的列

📅  最后修改于: 2022-05-13 01:55:42.860000             🧑  作者: Mango

在 PySpark DataFrame 中添加具有字面量值的列

在本文中,我们将看到如何在 PySpark Dataframe 中添加具有字面量值的列。

创建用于演示的数据框:

Python3
# import SparkSession from the pyspark
from pyspark.sql import SparkSession
 
# build and create the
# SparkSession with name "lit_value"
spark = SparkSession.builder.appName("lit_value").getOrCreate()
 
# create the spark dataframe with columns A,B
data = spark.createDataFrame([('x',5),('Y',3),
                            ('Z',5) ],['A','B'])
 
# showing the schema and  table
data.printSchema()
data.show()


Python3
# Import the lit() function
# from the pyspark.sql.functions
from pyspark.sql.functions import lit
 
# select all the columns from data
# table and insert new columns
# 'literal_values_1' with values 1
df2 = data.select('*' ,lit("1").alias("literal_values_1"))
  
# showing the schema and updated table
df2.printSchema()
df2.show()


Python3
# this will create a temp view of the table as lit_val
df2.createOrReplaceTempView("temp")
 
# select all the columns and rows
# from data table and insert new
# columns 'literal_values_2' with values 2
df2 = spark.sql("select *, 2 as literal_values_2 from temp")
 
# showing the schema and updated table
df2.printSchema()
df2.show()


Python3
# import the udf from pyspark
from pyspark.sql.functions import udf
 
# defining the data types of udf which is
# integer type
@udf("int")
 
# defining the lit_col() function which
# will return literal values to  data frame
def lit_col():
    return 3
 
# create new column as
# 'literal_values_3' with values 3
df2 = df2.withColumn('literal_values_3', lit_col())
 
# showing the schema and updated table
df2.printSchema()
df2.show()



输出:



方法一:使用 Lit()函数

在这里,我们可以使用 select 方法添加值为 1 的常量列 'literal_values_1'。 lit()函数将向所有行插入常量值。

使用 select() 方法选择表并传递参数第一个是列名,或“*”用于选择整个表,第二个参数传递具有常量值的 lit()函数。

蟒蛇3

# Import the lit() function
# from the pyspark.sql.functions
from pyspark.sql.functions import lit
 
# select all the columns from data
# table and insert new columns
# 'literal_values_1' with values 1
df2 = data.select('*' ,lit("1").alias("literal_values_1"))
  
# showing the schema and updated table
df2.printSchema()
df2.show()

输出:

方法二:使用SQL子句

在这个方法中,我们首先要创建表的临时视图,借助 createTempView 我们可以创建临时视图。这个 temp 的生命周期取决于 sparkSession 的生命周期。如果临时表不可用,则 CreateOrReplace 将创建临时表,如果可用则替换它。

然后在创建表后通过 SQL 子句选择表,它将所有值作为字符串

蟒蛇3



# this will create a temp view of the table as lit_val
df2.createOrReplaceTempView("temp")
 
# select all the columns and rows
# from data table and insert new
# columns 'literal_values_2' with values 2
df2 = spark.sql("select *, 2 as literal_values_2 from temp")
 
# showing the schema and updated table
df2.printSchema()
df2.show()

输出:

方法三:使用UDF(User-defined Functions)方法

这个函数允许我们根据我们的要求创建新函数,这就是为什么这也被称为用户定义函数。现在我们定义 UDF函数的数据类型并创建以新列的形式返回值的函数

蟒蛇3

# import the udf from pyspark
from pyspark.sql.functions import udf
 
# defining the data types of udf which is
# integer type
@udf("int")
 
# defining the lit_col() function which
# will return literal values to  data frame
def lit_col():
    return 3
 
# create new column as
# 'literal_values_3' with values 3
df2 = df2.withColumn('literal_values_3', lit_col())
 
# showing the schema and updated table
df2.printSchema()
df2.show()

输出: