col 函数如何知道我们引用的是哪个 DataFrame?

ris*_*hai 2 python scala apache-spark apache-spark-sql pyspark

我一直对 Spark 中的函数有点困惑col,无论是 Python 还是 Scala 中的函数。看起来:

df.col("zipcode")在 Scala 中相当于

df["zipcode"]spark.sql.functions.col("zipcode")Python中。

使用https://spark.apache.org/docs/latest/api/python/pyspark.sql.html#pyspark.sql.functions.col的最后一个用法让我感到困惑。如何spark.sql.functions.col知道我们指的是哪个Python DataFrame?我们只是传递列的名称。

小智 5

col函数指的是您正在执行转换(选择、连接等)的数据帧如果您想选择 DataFrame 列之一,您应该执行以下操作:

# you can import the col function directly
from pyspark.sql.functions import col 
 
# Imagine your df columns are: id, name, age

df.select(col("id"))
Run Code Online (Sandbox Code Playgroud)

这意味着您正在从数据帧“df”中选择列“id”。选择引用您的 DataFrame 对象。您正在将列对象传递给您正在使用的当前 DataFrame。