cancel
Showing results for 
Search instead for 
Did you mean: 
Data Engineering
Join discussions on data engineering best practices, architectures, and optimization strategies within the Databricks Community. Exchange insights and solutions with fellow data engineers.
cancel
Showing results for 
Search instead for 
Did you mean: 

NameError: name 'col' is not defined

MudassarA
New Contributor II

I m executing the below code and using Pyhton in notebook and it appears that the col() function is not getting recognized .

I want to know if the col() function belongs to any specific Dataframe library or Python library .I dont want to use pyspark api and would like to write code using sql dataframes API

Trying to run the below code and getting error -NameError: name 'col' is not defined

peopleDF = spark.read.parquet("/mnt/training/dataframes/people-10m.parquet") peopleDF.printSchema() peopleDF.show() peopleDF.select(col("firstName")).filter(col("firstName"))=="An"

As per SPARK doc

https://spark.apache.org/docs/latest/api/scala/index.html#org.apache.spark.sql.Column

df("columnName")            // On a specific `df` DataFrame.
col("columnName")           // A generic column not yet associated with a DataFrame.
col("columnName.field")     // Extracting a struct field
col("`a.column.with.dots`") // Escape `.` in column names.
$"columnName"               // Scala short hand for a named column.

1 REPLY 1

MOHAN_KUMARL_N
New Contributor II

@mudassar45@gmail.com

as the document describe generic column not yet associated. Please refer the below code.

display(peopleDF.select("firstName").filter("firstName = 'An'"))

Connect with Databricks Users in Your Area

Join a Regional User Group to connect with local Databricks users. Events will be happening in your city, and you won’t want to miss the chance to attend and share knowledge.

If there isn’t a group near you, start one and help create a community that brings people together.

Request a New Group