cancel
Showing results for 
Search instead for 
Did you mean: 
Data Engineering
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'"))

Welcome to Databricks Community: Lets learn, network and celebrate together

Join our fast-growing data practitioner and expert community of 80K+ members, ready to discover, help and collaborate together while making meaningful connections. 

Click here to register and join today! 

Engage in exciting technical discussions, join a group with your peers and meet our Featured Members.