Forum Discussion
ToddChitt
2 years agoSuper User
PySpark to rename a column in a dataframe
Hello. I am quite new to Spark Notebooks. I am using one to extract JSON data to save to tables in a Lakehouse. It works, but there are some slight issues. The data, being JSON, has nexted objects. I...
ToddChitt
2 years agoSuper User
Hello AndyDDC and thank you for the reply.
I tried your suggestion but it generated an error: ...name 'col; is not defined.
But from this website PySpark alias() Column & DataFrame Examples - Spark By {Examples} (sparkbyexamples.com)(which I think is about to become my new best friend 🙂 ) I added this line of code at the top of the block:
from pyspark.sql.functions import col
And that fixed it.
Another example from the site shows this syntax will work without the import statement above:
df.select ( df.Id.alias ( "Employee_Id" ),...
Thanks for your help.
- AndyDDC2 years agoMost Valuable Professional
Great to hear. And yes the Spark By Example website is awesome!