Lưu ý
Cần có ủy quyền mới truy nhập được vào trang này. Bạn có thể thử đăng nhập hoặc thay đổi thư mục.
Cần có ủy quyền mới truy nhập được vào trang này. Bạn có thể thử thay đổi thư mục.
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