Select all Spark columns except one
How to select every column except one in a Spark DataFrame using drop(), a filtered select, or SQL, plus the tradeoffs around case sensitivity, dotted names, joins and silent no-ops.
The short answer
For most Spark DataFrame work, calling drop() with the unwanted column name is the simplest and clearest way to keep everything except one column. It returns a new DataFrame and leaves the source data untouched. Building an explicit select() list is the better choice when you need a fixed column order, a reviewable list of output columns, or exact control over which of several same-named columns survives. The main tradeoff is that drop() with a string name silently does nothing when the name does not match, so a typo or a hidden character in a header can pass unnoticed.
Is excluding one column the right approach for your use?
Excluding a column works well when the schema is wide, mostly stable, and you only need to remove one or two fields such as an internal ID, a raw payload or a sensitive attribute before writing output. Its benefit is brevity: the code keeps working if new columns are added upstream.
That same benefit is the decisive tradeoff. Because the result is "everything else," any new upstream column flows straight into your output. If the result feeds a published table, an export file or a downstream contract, an explicit list of the columns you want is usually safer than a list of the columns you don't. Prefer the explicit list when:
- The output schema is consumed by other teams, tools or file formats that expect a fixed set of columns.
- You are removing data for privacy or compliance reasons and need to guarantee that nothing else sensitive appears later.
- Column order matters, for example when writing headerless delimited files.
- The DataFrame came from a join and contains duplicate column names.
What the transformation actually changes
Neither drop() nor select() modifies stored data. Spark DataFrames are immutable, so each call returns a new DataFrame with a different logical plan. Nothing is computed until an action such as write, count or show runs. With columnar sources such as Parquet, the optimizer can prune unused columns so they may not be read from storage at all; with row-based formats such as CSV, the full row is still parsed before the column is discarded.
The word "except" is ambiguous in Spark. DataFrame.exceptAll() and the SQL EXCEPT set operator remove rows that appear in another dataset; they do not remove columns. If you want to exclude a column, you need drop(), a filtered select(), or a SQL star-exclusion syntax where your engine supports it.
PySpark
# Remove one column
result = df.drop("email")
# Remove several columns
result = df.drop("email", "phone")
# Explicit list of everything except one column
keep = [c for c in df.columns if c != "email"]
result = df.select(*keep)
Scala
import org.apache.spark.sql.functions.col
val result = df.drop("email")
val keep = df.columns.filterNot(_ == "email").map(c => col(s"`$c`"))
val result2 = df.select(keep: _*)
SQL
Databricks SQL documents a star clause with an exclusion list, written as SELECT * EXCEPT (column_name) FROM table. Support in open-source Apache Spark SQL depends on the version, so check the SQL reference for the release you run before relying on it. Open-source Spark also offers a regex-based column selection when the spark.sql.parser.quotedRegexColumnNames setting is enabled, but it is harder to read and review than a DataFrame drop().
-- Databricks SQL (verify support in your Spark version)
SELECT * EXCEPT (email) FROM customers;
Behavior differences that affect the result
| Concern | drop("name") | Filtered select() list |
| Name not found | Silently ignored; no error | Column stays in the list; no error |
| Case sensitivity | Follows spark.sql.caseSensitive (off by default), so "Email" can match "email" | Python or Scala string comparison is always case-sensitive |
| Names containing dots | String is treated as a literal top-level name | Plain names with dots are parsed as nested paths unless wrapped in backticks |
| Duplicate names after a join | A string name can remove every column with that name; pass a column reference such as df1["id"] to target one | Selection by name can be ambiguous; use column references |
| Nested struct fields | Does not remove a field inside a struct | Rebuild the struct, or use Column.dropFields() in Spark 3.1 and later |
| Column order | Remaining columns keep their original order | Order follows your list |
The case-sensitivity row is the most common source of surprise: the two approaches can disagree on the same DataFrame. If your column names differ only by case, enable case-sensitive analysis or compare names consistently in both places.
Will the result remain usable after copying or sharing?
Two practical issues come up when snippets and column names move between documents, chat tools and editors.
First, copied code can pick up typographic quotation marks or non-breaking spaces from rich-text sources. Spark will reject curly quotes as a syntax error in most cases, but an invisible character inside a column name string may simply produce a name that matches nothing, and drop() will quietly leave the column in place. Paste snippets into a plain-text editor or retype the column name if the result looks unchanged.
Second, headers from delimited files can carry leading or trailing spaces, a byte-order mark on the first column, or non-ASCII characters that look identical to ASCII ones. Before dropping a column read from CSV or TSV, print the column list with repr() in Python so hidden characters become visible, and consider normalizing headers once at load time. A simple safeguard is to confirm the column count changed after the drop.
before = len(df.columns)
result = df.drop("email")
assert len(result.columns) == before - 1, [repr(c) for c in df.columns]
Because excluding a column changes only the schema of a DataFrame, questions about screen readers, visual reading order or search indexing do not apply to the transformation itself. They apply only to whatever report or export you later produce from the result.
Gaps and limits of this review
No version-specific test results or benchmarks were supplied for this article. The behavior described reflects documented Spark DataFrame semantics, and details such as SQL star-exclusion support and the handling of duplicate names can vary by release and platform. Verify against the documentation and a small test on the Spark version and environment you actually use, especially before applying a column exclusion to data that is shared externally or removed for compliance reasons.