columns
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.
Python
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.
Python
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.
Python
"state" in df.columns
# True
"salary" in df.columns
# False