Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Retrieves the names of all columns in the DataFrame as a list. The order of the column names in the list reflects their order in the DataFrame.
Returns
list
Examples
Retrieve column names of a DataFrame.
df = spark.createDataFrame(
[(14, "Tom", "CA"), (23, "Alice", "NY"), (16, "Bob", "TX")],
["age", "name", "state"]
)
df.columns
# ['age', 'name', 'state']
Use column names to project specific columns.
selected_cols = [col for col in df.columns if col != "age"]
df.select(selected_cols).show()
# +-----+-----+
# | name|state|
# +-----+-----+
# | Tom| CA|
# |Alice| NY|
# | Bob| TX|
# +-----+-----+
Check if a specific column exists in a DataFrame.
"state" in df.columns
# True
"salary" in df.columns
# False